New (C++) - Releasing Dynamically Allocated Memory

Releasing Dynamically Allocated Memory

Any memory dynamically allocated with new must be released with delete. The delete operator can be used in two ways: one for de-allocating memory allocated to arrays with new and the other for deleting single objects.

int *p_var = new int; delete p_var; p_var = 0; int *p_array = new int; delete p_array; p_array = 0;

Note that the compiler is not required to generate a diagnostic message for using the wrong delete. Furthermore, using the inappropriate deallocator will result in undefined behavior.

Read more about this topic:  New (C++)

Famous quotes containing the word memory:

    “Raising children is a spur-of-the-moment, seat-of-the-pants sort of deal, as any parent knows, particularly after an adult child says that his most searing memory consists of an offhand comment in the car on the way to second grade that the parent cannot even dimly recall.”
    —Anna Quindlen (b. 1952)