next up previous
Next: Classes Up: October 25 Previous: C++ overview


Dynamic allocation

In addition to C's malloc-like functions, and free, C++ has new and delete. These can provide convenient dynamic allocation. There is no garbage collection.

int *a, *b;

a = new int;  // new provides a pointer
*a = 100;
b = new int[*a]; // array of 100 ints
b[3] = 7;

delete a; // frees the allocated memory
delete [] b; // weird, eh?  frees the 100 ints.



Danny Heap 2002-12-16