In C++98 there are six flavors each of operator new
and operator delete
, so make certain that you're
using the right ones.
Here are quickie descriptions of operator new
:
void* operator new(std::size_t);
std::bad_alloc
on error.
This is what most people are used to using.
void* operator new(std::size_t, std::nothrow_t) noexcept;
operator new(std::size_t)
but if that throws,
returns a null pointer instead.
void* operator new[](std::size_t);
new
.
Calls operator new(std::size_t)
and so
throws std::bad_alloc
on error.
void* operator new[](std::size_t, std::nothrow_t) noexcept;
new
.
Calls operator new[](std::size_t)
but if that throws,
returns a null pointer instead.
void* operator new(std::size_t, void*) noexcept;
new
,
which does nothing except return its argument.
This function cannot be replaced.
void* operator new[](std::size_t, void*) noexcept;
new
,
which also does nothing except return its argument.
This function cannot be replaced.
They are distinguished by the arguments that you pass to them, like
any other overloaded function. The six flavors of
operator delete
are distinguished the same way, but none of them are allowed to throw
an exception under any circumstances anyhow. (The overloads match up
with the ones above, for completeness' sake.)
The C++ 2014 revision of the standard added two additional overloads of
operator delete
for “sized deallocation”,
allowing the compiler to provide the size of the storage being freed.
The C++ 2017 standard added even more overloads of both
operator new
and operator delete
for allocating and deallocating storage for overaligned types.
These overloads correspond to each of the allocating forms of
operator new
and operator delete
but with an additional parameter of type std::align_val_t.
These new overloads are not interchangeable with the versions without
an aligment parameter, so if memory was allocated by an overload of
operator new
taking an alignment parameter,
then it must be decallocated by the corresponding overload of
operator delete
that takes an alignment parameter.
Apart from the non-allocating forms, the default versions of the array
and nothrow operator new
functions will all result
in a call to either operator new(std::size_t)
or
operator new(std::size_t, std::align_val_t)
,
and similarly the default versions of the array and nothrow
operator delete
functions will result in a call to
either operator delete(void*)
or
operator delete(void*, std::align_val_t)
(or the sized versions of those).
Apart from the non-allocating forms, any of these functions can be
replaced by defining a function with the same signature in your program.
Replacement versions must preserve certain guarantees, such as memory
obtained from a nothrow operator new
being free-able
by the normal (non-nothrow) operator delete
,
and the sized and unsized forms of operator delete
being interchangeable (because it's unspecified whether
the compiler calls the sized delete instead of the normal one).
The simplest way to meet the guarantees is to only replace the ordinary
operator new(size_t)
and
operator delete(void*)
and
operator delete(void*, std::size_t)
functions, and the replaced versions will be used by all of
operator new(size_t, nothrow_t)
,
operator new[](size_t)
and
operator new[](size_t, nothrow_t)
and the corresponding operator delete
functions.
To support types with extended alignment you may also need to replace
operator new(size_t, align_val_t)
and
operator delete(void*, align_val_t)
operator delete(void*, size_t, align_val_t)
(which will then be used by the nothrow and array forms for
extended alignments).
If you do need to replace other forms (e.g. to define the nothrow
operator new
to allocate memory directly, so it
works with exceptions disabled) then make sure the memory it allocates
can still be freed by the non-nothrow forms of
operator delete
.
If the default versions of operator new(std::size_t)
and operator new(size_t, std::align_val_t)
can't allocate the memory requested, they usually throw an exception
object of type std::bad_alloc
(or some class
derived from that). However, the program can influence that behavior
by registering a “new-handler”, because what
operator new
actually does is something like:
while (true) { if (void* p = /* try to allocate memory */) return p; else if (std::new_handler h = std::get_new_handler ()) h (); else throw bad_alloc{}; }
This means you can influence what happens on allocation failure by
writing your own new-handler and then registering it with
std::set_new_handler
:
typedef void (*PFV)(); static char* safety; static PFV old_handler; void my_new_handler () { delete[] safety; safety = nullptr; popup_window ("Dude, you are running low on heap memory. You" " should, like, close some windows, or something." " The next time you run out, we're gonna burn!"); set_new_handler (old_handler); return; } int main () { safety = new char[500000]; old_handler = set_new_handler (&my_new_handler); ... }
Remember that it is perfectly okay to delete
a
null pointer! Nothing happens, by definition. That is not the
same thing as deleting a pointer twice.
std::bad_alloc
is derived from the base
std::exception
class,
see Exceptions.