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.

Read syntax diagramSkip visual syntax diagramdelete operator syntax
 
>>-+----+--delete--object_pointer------------------------------><
   '-::-'
 

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.

Read syntax diagramSkip visual syntax diagramdelete[] operator syntax
 
>>-+----+--delete--[--]--array---------------------------------><
   '-::-'
 

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 global ::operator delete() is used if:

The global ::operator delete[]() is used if:

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[]().

Related information



[ Top of Page | Previous Page | Next Page | Contents | Index ]