Namespaces
Variants
Views
Actions

delete expression

From cppreference.com
< cpp‎ | language
Revision as of 11:01, 6 May 2013 by Cubbi (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
delete expression
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

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

Syntax

::(optional)    delete    Template:sparam (1)
::(optional)    delete [] Template:sparam (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, Template:sparam must be a pointer or a class with a user-defined conversion function to a pointer, and its value must be either null or pointer to a non-array object created by a new-exression, or a pointer to a base subobject of a non-array object created by a new-exression (if it's anything else, the behavior is undefined).

For the second (array) form, Template:sparam must be a null pointer value or a pointer value previously obtained by an array form of new-expression.

If Template:sparam 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, 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 firstly looked up in the local class type scope and only if the lookup fails, the global namespace is looked up. If :: is present in the delete expression, only the global namespace is looked up. The prototype of the function must look like the following:

Template:ddcl list begin <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
void operator delete  (void *ptr);
</td>

<td class="t-dcl-nopad"> </td> <td > for the first version </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
void operator delete[](void *ptr);
</td>

<td class="t-dcl-nopad"> </td> <td > for the second version </td> </tr> Template:ddcl list end

Both these functions are implicitly declared in each translation unit. Also, implicit implementations are provided by the compiler by default, unless the program has explicitly implemented them. See this for more information.

If Template:sparam evaluates to a null pointer value, no destructors are called. 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.

If Template:sparam 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.

Keywords

delete