W3cubDocs

/C++

delete expression

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

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.

Explanation

For the first (non-array) form, expression must be a pointer to an 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. The pointed-to type of expression must be similar to the type of the object (or of a base subobject). 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 whose allocation function was not a non-allocating form (i.e. overload (10)). The pointed-to type of expression must be similar to the element type of the array object. If expression is anything else, including if it's a pointer obtained by the non-array form of new-expression, the behavior is undefined.

The result of the expression always has type void.

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 expression is not a null pointer and the deallocation function is not a destroying delete (since C++20), 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, whether or not an exception was thrown by any destructor, 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), unless the matching new-expression was combined with another new-expression (since C++14).

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 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 at least one of the deallocation functions is a destroying delete, all non-destroying deletes are ignored.
(since C++20)
  • If the type's alignment requirement exceeds __STDCPP_DEFAULT_NEW_ALIGNMENT__, alignment-aware deallocation functions (with a parameter of type std::align_val_t) are preferred. For other types, the alignment-unaware deallocation functions (without a parameter of type std::align_val_t) are preferred.
    • If more than one preferred functions are found, only preferred functions are considered in the next step.
    • If no preferred functions are found, the non-preferred ones are considered in the next step.
  • If only one function is left, that function is selected.
(since C++17)
  • If the deallocation functions that were found are class-specific, size-unaware class-specific deallocation function (without a parameter of type std::size_t) is preferred over size-aware class-specific deallocation function (with a parameter of type std::size_t).
  • Otherwise, lookup reached global scope, and:
    • If the type is complete and if, for the array form only, the operand is a pointer to a class type with a non-trivial destructor or a (possibly multi-dimensional) array thereof, the global size-aware global function (with a parameter of type std::size_t) is selected.
    • Otherwise, it is unspecified whether the global size-aware deallocation function (with a parameter of type std::size_t) or the global size-unaware deallocation function (without a parameter of type std::size_t) is selected.
(since C++14)

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::size_t argument. The alignment requirement is passed as the optional std::align_val_t argument. (since C++17).

If expression 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 expression evaluates to a pointer to a 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.

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 an 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)

Keywords

delete.

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
CWG 288 C++98 for the first form, the static type of the
operand was compared with its dynamic type
compare the static type of the object
to be deleted with its dynamic type
CWG 353 C++98 whether the deallocation function will be invoked if
the destructor throws an exception was unspecified
always invoked
CWG 599 C++98 the first form could take a null pointer of
any type, including function pointers
except pointers to object types,
all other pointer types are rejected
CWG 2474 C++98 deleting a pointer to an object of a similar but
different type resulted in undefined behavior
made well-defined
CWG 2624 C++98 pointers obtained from non-allocating
operator new[] could be passed to delete[]
prohibited

See also

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/language/delete