Namespaces
Variants
Actions

delete expression

From cppreference.com
< cpp‎ | language
 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
delete expression
Classes
Class-specific function properties
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 

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

Contents

[edit] Syntax

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

[edit] Explanation

Given the pointer evaluated from expression (after possible conversions) as ptr.

1) ptr must be one of
  • a null pointer,
  • a 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 ptr must be similar to the type of the object (or of a base subobject). If ptr is anything else, including if it is a pointer obtained by the array form of new-expression, the behavior is undefined.
2) ptr must be a null pointer or a pointer whose value is 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 ptr must be similar to the element type of the array object. If ptr is anything else, including if it is a pointer obtained by the non-array form of new-expression, the behavior is