1.14.2.  Operators new and delete

[ fromfile: pointers.xml id: newdelete ]

C++ has a mechanism that permits storage to be allocated dynamically at runtime. This means that the programmer does not need to anticipate the memory needs of a program in advance and make allowances for the maximum amount of memory that might be needed by the program. Dynamic allocation of storage at runtime is a powerful tool that helps to build programs that are efficient and flexible.

The new operator allocates storage from the heap (also known as dynamic storage) and returns a pointer to the newly allocated object. If for some reason it is not possible for the memory to be allocated, an exception is thrown. (Section 22.9)

The delete operator releases dynamically allocated memory and returns it to the heap. delete should be applied only to pointers returned by new, or to null pointers. Heap memory that is no longer needed should be released for reuse. Failure to do so can result in crippling memory leaks.

In general, the code that calls new should document, or be physically located near, the code that frees the memory. The goal is to keep memory management code as simple and reliable as possible.

Dereferencing a null, deleted, or uninitialized pointer causes a runtime error, usually a segmentation fault or, in Windows, a general protection fault (GPF). It is the responsibility of the programmer to make sure that this cannot happen. We will discuss techniques to ensure that such errors are avoided.

[Warning]Warning

The ability to manage memory gives the programmer great power. But with great power comes great responsibility.

The syntax of the new and delete operators is demonstrated in the code fragment shown in Example 1.24.

Example 1.24. src/pointers/newdelete/ndsyntax.cpp

{
 int* ip = 0;   1
 ip = new int;  2
 int* jp = new int(13);     3
 [...]      
 delete ip;    4
 delete jp;
}

1

null pointer

2

allocate space for an int

3

allocate and initialize

4

Without this, we have a memory leak.

[Note]Note

Qt, the Standard Library, and Boost.org each provide a variety of classes and functions to help manage and clean up heap memory. In addition to container classes, each library has one or more smart pointer class. A smart pointer is an object that stores and manages a pointer to a heap object. It behaves much like an ordinary pointer except that it automatically deletes the heap object at the appropriate time. Qt has QPointer, the standard library has std::auto_ptr, and Boost has a shared_ptr. Using one of these classes makes C++ memory management much easier and safer than it used to be.