Dynamic Memory Allocation: new and delete (C++)


Match word(s).

If you have any questions or comments,
please visit us on the Forums.

FAQ > Explanations of... > Dynamic Memory Allocation: new and delete (C++)

This item was added on: 2003/02/12

Credit: shtarker

Despite the long name, this is a simple concept. All you are doing is allocating some more space for you to use at runtime. There are two keywords that you need to know: "new" and "delete". The keyword new will allocate a chunk of memory within your program and delete will deallocate it. You will probably also want to keep track of these chunks by using pointers. Getting the space is done as follows (notice how similar the syntax is to that of an array):

int* tree = new int[10];

This code will create a continuous block of memory large enough to store 10 integers and set the pointer tree to point to the beginning of that block. Accessing the block of memory is done just like you would an array:

tree[6] = 87;

The above piece of code would set the seventh integer in the block to the value of 87. After you've finished with the memory you can get rid of it by deleting it:

delete []tree;

Fortunately C++ keeps track of the size of the memory created for tree and knows exactly how much space to deallocate. It is also important to note that using the new keyword will create a continuous block of memory, but that there is always a possibility that you will request a memory block that is too large. The default response is for C++ to throw an exception, but this is much deeper than most programmers want to go. An easier method is found from the new header file. The new header file gives you the function "set_new_handler". This will set a default function to be called when you run out of memory. Here's a simple example of how to use it (Note: Being lazy I've just used the constant of 900000000 to deplete the memory. If the program actually works on your computer, you may need to increase this value):


#include <iostream> 
#include <cstdlib> 
#include <new> 

void out_of_memory(void)
{
  std::cerr << "Out of memory. Please go out and buy some more." << std::endl;
  exit(1);
}

int main(void)
{
  std::set_new_handler(out_of_memory);

  int *tree = new int[900000000];
}


Of course when this program actually does run out of memory, it simply exits with the return value of one. In practice, you may want to have out_of_memory actually perform something useful. On a final note, I will explain how to create multiple dimensional arrays dynamically. There's a really simple trick to it, so I'll start with some code:


int **forest = new int *[10];
for (int i = 0; i < 10; i++)
{
  forest[i] = new int[4];
}


What's going on here you ask? Well, it's actually surprisingly simple. The ** means that forest is actually a pointer to a pointer which pointing to an integer (ok so its not all that simple after all). Just think of it this way: forest is pointing to a block of memory containing ten other pointers. Each of these other pointers then point to a block of memory that holds four actual integers. The total effect of this is enough space to hold 40 different integers, or a 10 by 4 array.

For more on memory management in C++, see dynamic memory management in C++, understanding heaps and virtual memory and overloading operator new.
Credit: shtarker

Script provided by SmartCGIs