C++ Primer For Java Programmers


Dynamic Memory

<< Pointers | Table of Contents | Arrays >>
The variables we have been using up to this point have been statically allocated either in the data section of the executable program at compile time (global variables), or on the stack when a function is executed (local variables). Most programming languages also allow you to create dynamic variables allocated on the "heap" at run-time.

Dynamic Allocation

To create or allocate a dynamic variable, you use the new operator in a fashion similar to that in Java. Dynamic variables must be assigned to a pointer variable of the same data type. Consider the following code segment

(:source lang=cpp:)
int x = 5;      // static variable.
int *p;         // static variable.
p = new int;    // allocate a dynamic int variable.

which allocates two static variables x and p and allocates a dynamic int variable and assigns it to pointer p. The result appears as follows

After a dynamic variable is created, it can be used just like any other variable, the only difference is that it must be indirectly accessed using the pointer to which it was assigned. We can initialize the newly created dynamic variable using the statement

(:source lang=cpp:)
*p = 40;

which produces

Once the dyanmic variable has been initialized, it can then be used. The following statement uses the value in the dynamic variable pointed to by p

(:source lang=cpp:)
x = x + *p

This statement modifies the value of x as illustrated below

The new operator can be used to allocate a dynamic variable of any data type, built-in or user-defined.

Garbage Collection

Unlike Java and Python, C++ does not provide garbage collection and thus does not automatically deallocate dynamic variables when you are finished with them. You are responsible for deallocating any dynamic variable you create when you no longer need it.

(:note warn:) Failure to deallocate dynamic memory can result in memory leaks which can lead to your program running out of memory. Be careful when allocating dynamic variables and make certain they are destroyed when no longer needed. (:noteend:)

To delete a previously allocated dynamic variable, you use the delete operator along with the pointer which references the dynamic variable you wish to deallocate

(:source lang=cpp:)
delete p;
p = NULL;

When a dynamic variable is destroyed, the corresponding pointer variable is not modified. It is good practice, however, to set the pointer to NULL to prevent the accidental resuse later in your program.

(:note warn:) Attempting to deallocate a static variable will result in a run-time error. Only variables dynamically allocated with new can be destroyed with delete. (:noteend:)

Standard C Memory Allocation

The new and delete operators are specific to C++. In C, dynamic memory is allocated and released using the malloc() and free() methods respectively. To use either function, you must include the cstdlib standard module.

Allocation. To allocate dynamic memory using the standard C approach, we call malloc()

(:source lang=cpp:)
int *pInt = (int *) malloc( sizeof( int ) );

The malloc() function takes a single argument which indicates the number of bytes to allocate.

  • We typically use the sizeof() function to obtain the number of bytes required by the given type.
  • The value returned by sizeof() is passed to malloc() which then allocates a dynamic variable of that size.

When the dynamic variable is allocated, malloc() returns its address as a void pointer. The void pointer must be typecast to a pointer of the appropriate type.

Deallocation. To deallocate dynamic memory previoulsy allocated with malloc(), you must use the free() function

(:source lang=cpp:)
free( pInt );
pInt = NULL;


<< Pointers | Table of Contents | Arrays >>

Print - Changes - Search
Last modified: April 30, 2007, at 10:00 PM.