How does C++ free the memory when a constructor throws an exception and a custom new is used
Posted
by
Joshua
on Stack Overflow
See other posts from Stack Overflow
or by Joshua
Published on 2013-10-30T15:19:18Z
Indexed on
2013/10/30
15:54 UTC
Read the original article
Hit count: 199
I see the following constructs:
new X
will free the memory if X
constructor throws.
operator new()
can be overloaded.
The canonical definition of an operator new overload is void *operator new(heap h)
and the corrisponding operator delete
.
The most common operator new overload is pacement new, which is void *operator new(void *p) { return p; }
You almost always cannot call delete
on the pointer given to placement new
.
This leads to a single question.
How is memory cleaned up when X
constructor throws and an overloaded new
is used?
© Stack Overflow or respective owner