Namespaces
Variants
Views
Actions

delete expression

From cppreference.com
 
 
C++ language
General topics
Preprocessor
Comments
Keywords
ASCII chart
Escape sequences
History of C++
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)
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

[edit] Syntax

::(optional)    delete    expression (1)
::(optional)    delete [] expression (2)

[edit] Explanation

Destructs objects, previously constructed by new expression and releases the obtained memory area. The expression must result in a pointer value that was returned by previous call to the new expression. For single allocated objects the first version of the expression must be used, whereas for arrays the second version must be used. There is no delete expression deallocating objects initialized by placement new, deallocation function and destructor must be explicitly called.

The memory is deallocated by an 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:

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

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.

[edit] Keywords

delete