Can anybody explain what will happen if a new is overloaded but corresponding delete is not loaded in C++?
-
3http://stackoverflow.com/questions/4421706/operator-overloading/4421791#4421791 maybe this will help – S.P. Oct 09 '12 at 17:58
1 Answers
This is only an issue when the object construction throws an exception, and it is described in C++11 5.3.4/18:
If no unambiguous matching deallocation function can be found, propagating the exception does not cause the object’s memory to be freed. [ Note: This is appropriate when the called allocation function does not allocate memory; otherwise, it is likely to result in a memory leak. —end note ]
Example:
T * p = new (true, 'x', Blue) T("Jim");
If the constructor of T
throws, we need an overload operator delete(void *, bool, char, enum Color)
, either at namespace scope or as a static member of T
, and if this function does not exist, then no deallocation function is called.
As the note says, in the case of placement-new functions which are essentially no-ops, this may not be a problem. However, if the allocation function does non-trivial work, then there'll be no matching clean-up function.

- 464,522
- 92
- 875
- 1,084