Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
new
bad_alloc
new_handler
nothrow
nothrow_t
set_new_handler
global namespace:
operator delete
operator delete[]
operator new
operator new[]


operator delete

function
<new>
void operator delete (void* ptr) throw ();
void operator delete (void* ptr, const std::nothrow_t& nothrow_constant) throw();
void operator delete (void* ptr, void* voidptr2) throw();

Deallocate storage space

The first and second versions deallocate the memory block pointed by ptr (if not-null), releasing the storage space previously allocated to it by a call to operator new and making that pointer location invalid.

The third version does nothing.

The second and third versions cannot be implicitly called by the operator expression (the delete operator calls once the function operator delete for each of its arguments). Although they can be called explicitly as operator new function calls, their default definitions serve no particular purpose - they are provided as counterparts for the operator new functions and called accordingly when done automatically.

Global dynamic storage operator functions are special in the standard library:
  • All three versions of operator delete are declared in the global namespace, not in the std namespace.
  • The first and second versions are implicitly declared in every translation unit of a C++ program: The header <new> does not need to be included for them to be present.
  • The first and second versions are also replaceable: A program may provide its own definition, that replaces the default one, to produce the result described above.

operator delete can be called explicitly as a regular function, but in C++, delete is an operator with a very specific behavior: An expression with the delete operator, first calls the appropriate destructor (if needed), and then calls function operator delete to release the storage.

Parameters

ptr
Either a null pointer, or a pointer to a memory block to be released, type-casted to a void*.
This pointer value should have been returned by a previous call to operator new.
nothrow_constant
The constant nothrow.
In the default definition, this parameter is ignored, thus the nothrow version performs the exact same functionality as the default version. But both exist, and both may be redefined separately.
nothrow_t is the type of constant nothrow.
voidptr2
A void pointer. The value is ignored in the default definition.

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// operator delete example
#include <iostream>
#include <new>
using namespace std;
struct myclass {
  myclass() {cout <<"myclass constructed\n";}
  ~myclass() {cout <<"myclass destroyed\n";}
};
int main () {
  myclass * pt;
  pt = new myclass;
  delete pt;
  return 0;
}


Output:

myclass constructed
myclass destroyed

See also