Home > Articles > Security > Network Security

Memory Hygiene in C and C++: Safe Programming with Risky Data

Cameron Laird
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Core C++: A Software Engineering Approach

Like this article? We recommend
Core C++: A Software Engineering Approach

Memory management is scary. It should be: A lot can go wrong—often very wrong. But a moderately experienced C or C++ programmer can learn and understand memory hazards completely. Once you have that knowledge, you should feel only confidence, not fear.

When you read a news alert about a new exploit having been found that "cracks" a widely used application, a crucial memory error is likely to be at the base of the problem. When a program "ages"—when it can't be left running more than a day without crashing, slowing down catastrophically, or interfering with other programs—that's likely to be a memory fault.

This is a serious point, and one that's hard for non-programmers to understand. Outsiders think we spend our time doing mathematical formulas or teaching computers to solve Aristotelian syllogisms ("Socrates is a man, therefore it's time to launch the missile"). This isn't true, of course; a great deal of our time goes to such tedium as laying out data structures efficiently, or incrementing by one. Because human memory works on an associative model, though, civilians don't have the background to appreciate this work. Memory management is much of what makes C programming difficult, and, from what I can tell, at the heart of many (perhaps most) reported security exploits.

I often work with a range of so-called high-level languages, almost all of which share a secret: A big part of their productivity advantage over C is simply the convenience of their memory management. One of the reasons that this is so important is the poor locality of memory problems. Traditional debugging techniques for correction of arithmetic or logical errors help little when tracking down memory problems, because it so often happens that a memory mismanagement in one module produces only intermittent symptoms, and those symptoms may appear in a distant module.

The good news, though, is that it's possible to manage memory correctly. This article is the first in a three-part series on how to acquire that skill. We'll look at recipes that help you code more secure and reliable C-based applications.

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jennifer  BortelWin FREE iPhone Developer Books and Videos- Introducing @InformIT Giveaways
By Jennifer Bortel on February 5, 2010 No Comments

Apples’s recent iPad announcement made our hearts flutter so we couldn’t resist making an announcement of our own!

Today marks the first ever @InformIT Giveaway!

We’ll regularly post a video like this one profiling spectacular prizes we’re giving away—from books and videos to T-shirts and other exciting stuff. Check out the video below to see the giveaways for today, and then scroll down for more prize details and instructions on how to win them!

Dustin Sullivan"Every OSX developer should have this book on their desk."
By Dustin Sullivan on February 1, 2010 No Comments

That was the sentence Mike Riley ended his recent Dr Dobb's CodeTalk review of Cocoa Programming Developer's Handbook with.

David ChisnallCocoa Tip of the Day, 1/29/10
By David Chisnall on January 29, 2010 No Comments

Don't ignore old versions of OS X.

See All Related Blogs

Informit Network