How does C++ free the memory when a constructor throws an exception and a custom new is used
- by Joshua
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?