cpp/language/delete

Destroys object(s) previously allocated by the and releases obtained memory area.

Syntax
@1@ Destroys one non-array object created by a @2@ Destroys an array created by a

Explanation
For the first (non-array) form, must be a pointer to an object type or a class type  to such pointer, and its value must be either null or pointer to a non-array object created by a, or a pointer to a base subobject of a non-array object created by a. The pointed-to type of must be  to the type of the object (or of a base subobject). If is anything else, including if it is a pointer obtained by the array form of, the behavior is.

For the second (array) form, must be a null pointer value or a pointer value previously obtained by an array form of  whose allocation function was not a non-allocating form (i.e. overload ). The pointed-to type of must be  to the element type of the array object. If is anything else, including if it's a pointer obtained by the non-array form of, the behavior is.

The result of the expression always has type.

If the object being deleted has incomplete class type at the point of deletion, and the complete class has a non-trivial destructor or a deallocation function, the behavior is undefined.

If is not a null pointer, the  expression invokes the  (if any) for the object that's being destroyed, or for every element of the array being destroyed (proceeding from the last element to the first element of the array).

After that, whether or not an exception was thrown by any destructor, the delete expression invokes the deallocation function: either (for the first version of the expression) or  (for the second version of the expression).

The deallocation function's name is in the scope of the dynamic type of the object pointed to by, which means class-specific deallocation functions, if present, are found before the global ones. If is present in the  expression, only the global namespace is examined by this lookup. In any case, any declarations other than of usual deallocation functions are discarded.

If lookup finds more than one deallocation function, the function to be called is selected as follows (see deallocation function for a more detailed description of these functions and their effects):


 * If the deallocation functions that were found are class-specific, size-unaware class-specific deallocation function (without a parameter of type std) is preferred over size-aware class-specific deallocation function (with a parameter of type std).

The pointer to the block of storage to be reclaimed is passed to the deallocation function that was selected by the process above as the first argument. The size of the block is passed as the optional std argument.

If evaluates to a null pointer value, no destructors are called, and the deallocation function may or may not be called (it's unspecified), but the default deallocation functions are guaranteed to do nothing when passed a null pointer.

If evaluates to a pointer to a base class subobject of the object that was allocated with, the destructor of the base class must be , otherwise the behavior is undefined.