1 2 3 4 5 6 7 8 9 10 11 12

Dynamic Storage management


In C programs can dynamically allocate and release blocks
of memory by calling
malloc, calloc, realloc and
free. Although C++ also has access to these functions
it provides two new operators (not functions) called
new
and delete(keywords) which allocate and release
space. Thus


int* int_ptr; int* int_array;

int_ptr = newint;

// allocates memory for an int

int_array = newint [100];

// allocates memory for
// an array of 100 ints

newreturns a NULL pointer if the requested memory cannot
be allocated. The
deleteoperator requires a pointer
as its operand.

delete int_ptr;


delete [] int_array;


March 2,1998

// releases memory pointed
// to by int_ptr
// de-allocates the array

Page 11

C201 TAM/AGS