operator delete, operator delete
Defined in header
void operator delete ( void* ptr );
void operator delete( void* ptr );
void operator delete ( void* ptr, const std::nothrow_t& );
void operator delete( void* ptr, const std::nothrow_t& );
void operator delete ( void* ptr, void* );
void operator delete( void* ptr, void* );
Deallocates storage. These allocation functions are called by delete-expressions to deallocate memory after destructing dynamic objects.
In all cases, if
ptr is a null pointer, the deallocation function does nothing.
Replacing and overloading
The versions (1-4) are implicitly declared in each translation unit even if the
<new> header is not included. These functions are replaceable: a user-provided non-member function with the same signature replaces the implicit version. At most one replacement may be provided for each of the four implicit deallocation functions. Also, program can define class member versions of these functions or define allocation functions with different signatures (except 5-6) are not replaceable).
The deallocation function can be replaced/overloaded in two ways:
- in the global scope: in order to call it, the signature of the overloaded allocation functions must be visible at the place of deallocation, except for implicitly declared default deallocation functions. This allocation function will be used for all deallocations with corresponding parameters in the current program
- in the local scope: the overloaded
operator deletemust be static public member function of the class. This deallocation function will be used only for deallocations of that particular class.
During compilation, each
delete expression looks up for appropriate deallocation function's name firstly in the class scope and after that in the global scope. It can be instructed to skip the first step. Note, that as per overloading rules, any deallocation functions declared in class scope hides all global deallocation functions. For more information see delete expression.
|ptr||-||pointer to a memory area to deallocate or a null pointer|
| allocation functions |
| releases uninitialized storage |
| deallocates memory |