I l@ve RuBoard Previous Section Next Section

Solution

graphics/bulb_icon.gif

The following table summarizes a C++ program's major distinct memory areas. Note that some of the names (for example, "heap") do not appear as such in the standard; in particular, "heap" and "free store" are common and convenient shorthands for distinguishing between two kinds of dynamically allocated memory.

Table 1. C++'s Memory Areas
Memory Area Characteristics and Object Lifetimes
Const Data

The const data area stores string literals and other data whose values are known at compile-time. No objects of class type can exist in this area.

All data in this area is available during the entire lifetime of the program. Further, all this data is read-only, and the results of trying to modify it are undefined. This is in part because even the underlying storage format is subject to arbitrary optimization by the implementation. For example, a particular compiler may choose to store string literals in overlapping objects as an optional optimization.

Stack

The stack stores automatic variables. Objects are constructed immediately at the point of definition and destroyed immediately at the end of the same scope, so there is no opportunity for programmers to directly manipulate allocated but uninitialized stack space (barring willful tampering using explicit destructors and placement new).

Stack memory allocation is typically much faster than for dynamic storage (heap or free store) because each stack memory allocation involves only a stack pointer increment rather than more-complex management.

Free Store

The free store is one of the two dynamic memory areas allocated/freed by new/delete.

Object lifetime can be less than the time the storage is allocated. That is, free store objects can have memory allocated, without being immediately initialized, and they can be destroyed, without the memory being immediately deallocated. During the period when the storage is allocated but outside the object's lifetime, the storage may be accessed and manipulated through a void*, but none of the proto-object's nonstatic members or member functions may be accessed, have their addresses taken, or be otherwise manipulated.

Heap

The heap is the other dynamic memory area allocated/freed by malloc()/free() and their variants.

Note that while the default global operators new and delete might be implemented in terms of malloc() and free() by a particular compiler, the heap is not the same as free store, and memory allocated in one area cannot be safely deallocated in the other.

Memory allocated from the heap can be used for objects of class type by placement new construction and explicit destruction. If so used, the notes about free store object lifetime apply similarly here.

Global/Static

Global or static variables and objects have their storage allocated at program startup, but may not be initialized until after the program has begun executing. For instance, a static variable in a function is initialized only the first time program execution passes through its definition.

The order of initialization of global variables across translation units is not defined, and special care is needed to manage dependencies between global objects (including class statics). As always, uninitialized proto-objects' storage may be accessed and manipulated through a void*, but no nonstatic members or member functions may be used or referenced outside the object's actual lifetime.

It's important to distinguish between the "heap" and the "free store," because the standard deliberately leaves unspecified the question of whether these two areas are related. For example, when memory is deallocated via ::operator delete(), the final note in section 18.4.1.1 of the C++ standard states:

"It is unspecified under what conditions part or all of such reclaimed storage is allocated by a subsequent call to operator new or any of calloc, malloc, or realloc, declared in <cstdlib>."

Further, it is unspecified whether new/delete is implemented in terms of malloc/free in a given implementation. It is specified, however, that malloc/free must not be implemented in terms of new/delete, according to 20.4.6, paragraphs 3 and 4:

"The functions calloc(), malloc(), and realloc() do not attempt to allocate storage by calling ::operator new().

"The function free() does not attempt to deallocate storage by calling ::operator delete()."

Effectively, the heap and the free store behave differently and are accessed differently, so be sure to use them differently.

Guideline

graphics/guideline_icon.gif

Understand the five major distinct memory stores, why they're different, and how they behave: stack (automatic variables); free store (new/delete); heap (malloc/free); global scope (statics, global variables, file scope variables, and so forth); const data (string literals, and so forth).


Guideline

graphics/guideline_icon.gif

Prefer using the free store (new/delete). Avoid using the heap (malloc/free).


    I l@ve RuBoard Previous Section Next Section