delete expression

From cppreference.com
< cpp‎ | language
 
 
 
 

Destructs object(s) previously allocated by the new expression and releases obtained memory area

Contents

[edit] Syntax

::(optional)    delete    expression (1)
::(optional)    delete [] expression (2)
1) Destroys one non-array object created by a new-expression
2) Destroys an array created by a new[]-expression

[edit] Explanation

For the first (non-array) form, expression must be a pointer to a complete object type or a class type contextually implicitly convertible to such pointer, and its value must be either null or pointer to a non-array object created by a new-expression, or a pointer to a base subobject of a non-array object created by a new-expression. If expression is anything else, including if it is a pointer obtained by the array form of new-expression, the behavior is undefined.

For the second (array) form, expression must be a null pointer value or a pointer value previously obtained by an array form of new-expression. If expression is anything else, including if it's a pointer obtained by the non-array form of new-expression, the behavior is undefined.

If expression is not a null pointer, the delete expression invokes the destructor (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, unless the matching new-expression was combined with another new-expression (since C++14) the delete expression invokes the deallocation function, either operator delete (for the first version of the expression) or operator delete[] (for the second version of the expression).

The deallocation function's name is looked up in the scope of the dynamic type of the object pointed to by expression, which means class-specific deallocation functions, if present, are found before the global ones. If :: is present in the delete expression, only the global namespace is looked up. The pointer to the dynamic type is passed to the deallocation function found. The prototype of the function must look like the following:

void operator delete  (void *ptr);
for the first version
void operator delete[](void *ptr);
for the second version

When overridden in class scope (and, since C++14, in global scope as well), optional second parameter may be used, in which the delete-expression will pass the size of the object that is being deallocated.

void operator delete  (void *ptr, std::size_t sz);
for the first version
void operator delete[](void *ptr, std::size_t sz);
for the second version

These functions are implicitly declared in each translation unit and implicit implementations are provided by the compiler by default, unless the program has explicitly implemented them. See deallocation functions for more information.

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

(until C++14)

If expression evaluates to a null pointer value, no destructors are called, and the deallocation function is not called.

(since C++14)

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

[edit] Notes

A pointer to void cannot be deleted because it is not a pointer to a complete object type.

Because a pair of brackets following the keyword delete is always interpreted as the array form of delete, a lambda-expression with empty capture list immediately after delete must be enclosed in parentheses.

// delete []{return new int; }(); // parse error
delete ([]{return new int; })(); // OK
(since C++11)

[edit] Keywords

delete