Destroys object(s) previously allocated by the new-expression and releases obtained memory area.
[edit] Syntax::
(optional) delete
expression (1) ::
(optional) delete[]
expression (2) [edit] Explanation
Given the pointer evaluated from expression (after possible conversions) as ptr.
1) ptrmust be one of
The pointed-to type of
ptrmust be
similarto the type of the object (or of a base subobject). If
ptris anything else, including if it is a pointer obtained by the array form of
new-expression, the behavior is
undefined.
2) ptrmust be a null pointer or a pointer whose value is previously obtained by an array form of
new-expressionwhose
allocation functionwas not a non-allocating form (i.e. overload
(10)).
The pointed-to type of
ptrmust be
similarto the element type of the array object. If
ptris anything else, including if it is a pointer obtained by the non-array form of
new-expression, the behavior is
undefined.
The result of the delete-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(until C++26)the program is ill-formed(since C++26).
If ptr 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 is being destroyed, or for every element of the array being destroyed (proceeding from the last element to the first element of the array). The destructor must be accessible from the point where the delete-expression appears.
After that, whether or not an exception was thrown by any destructor, the delete-expression invokes the deallocation function: either operator delete (first version) or operator delete[] (second version), 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 ptr, 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 any deallocation function is found, the function to be called is selected as follows (see deallocation function for a more detailed description of these functions and their effects):
__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.The selected deallocation function must be accessible from the point where the delete-expression appears, unless the deallocation function is selected at the point of definition of the dynamic typeâs virtual destructor.
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 ptr is 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 ptr is 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.
[edit] NotesA pointer to void cannot be deleted because it is not a pointer to an object type.
Because a pair of brackets following the keyword delete is always interpreted as the array form of a delete-expression, 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) [edit] Keywords [edit] 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 theRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4