C++ Memory Management

Java relies on two important design features to manage memory used by the program:

  1. Variables never actually contain objects, they contain references. Assignment and parameter passing are always done by copying these references, not by copying the objects themselves.
  2. The language provides garbage collection. This means the system will automatically release the storage for objects and other variables when no longer in use.

The C++ designers judge that memory management is not suited to a one-size-fits-all solution. The programmer should solve it in the best way for the problem at hand, and C++ provides several tools for this purpose. This is not complete insanity. Automated garbage collection is very expensive, and can cause great variation in the running times of a program's operations. This is a problem for real-time systems, which need predictable run-times.

On the other hand, the C++ approach is more trouble for the programmer, and introduces all sorts of pitfalls that simply don't exist in Java. But it follows from the differences in design philosophies. (In the case of C++, the first three design goals are “Run fast,” “Run really fast,” and “Don't do anything to slow down the program.”)

C++ objects and other variables are stored in one of three places. The static area is allocated at compile time, and includes things declared static, along with storage for constants and other odds and ends known before the program starts. Normal variables are allocated on the stack, along with other record-keeping data used when calling functions. When the program starts a function, or enters any curly-braced block, it allocates some space on the stack, which is reclaimed when the block is left. The third storage area, which we haven't used much so far, is called the heap. Space is allocated and returned to the heap by explicit operations in the program. More specifically:

Though C++ makes memory management the programmer's responsibility, it does provide some tools to do the job. Of particular interest:

In a language like Java, an object is automatically freed after there are no more references to it. In C++, the programmer must arrange to free the memory before that last reference to it is lost. To help think about it, one particular reference is thought to “own” the allocated memory. This is not something declared in the program, it's just part of the programmer's thinking. The owning reference is the one responsible to see that the object is deleted at the right time. Choose an owning reference, keep track of where it is, and make sure each object allocated on the heap has exactly one.

One note about the term heap. Don't confuse it with the data structure. Our “heap” refers to a a region of memory, having no specific layout. The name is probably chosen to contrast with the more ordered “stack.”