delete expressions (C++ only)
The delete operator destroys the object created with new by deallocating the memory associated with the object.
The delete operator has a void return type.
The operand of delete must be a pointer returned by new, and cannot be a pointer to constant. Deleting a null pointer has no effect.
The delete[] operator frees storage allocated for array objects created with new[]. The delete operator frees storage allocated for individual objects created with new.
The result of deleting an array object with delete is undefined, as is deleting an individual object with delete[]. The array dimensions do not need to be specified with delete[].
The result of any attempt to access a deleted object or array is undefined.
If a destructor has been defined for a class, delete invokes that destructor. Whether a destructor exists or not, delete frees the storage pointed to by calling the function operator delete() of the class if one exists.
- The class has no operator delete().
- The object is of a nonclass type.
- The object is deleted with the ::delete expression.
- The class has no operator delete[]()
- The object is of a nonclass type
- The object is deleted with the ::delete[] expression.
The default global operator delete() only frees storage allocated by the default global operator new(). The default global operator delete[]() only frees storage allocated for arrays by the default global operator new[]().