placement new and delete
There are two separate notions in C++:
The new/delete operators.
New/Delete expressions.
The operators allocate and deallocate memory. The new
expression constructs objects. The delete
expression sometimes destroys an object and calls the operator.
Why "sometimes"? Because it depends on the expression. The naked, global new
first calls operator-new to allocate memory and then constructs the object; the global delete
calls the destructor and deallocates the memory. But all other overloads of new
and delete
are different:
- An overloaded new expression calls an overloaded new operator to allocate memory and then proceeds to construct the object.
- However, there is no such thing as an overloaded delete expression, in particular there is no "placement-delete": Instead, you have to call the destructor manually.
New/Delete operators still have to be overloaded in matching pairs, because the matching delete operator is called when an object constructor throws an exception. However, there is no automatic way to invoke the destructor for an object that has been allocated with an overloaded new
operator, so you have to do that yourself.
As the first and most basic example, consider the placement-new operator, which is mandated to take the form void * operator new (size_t, void * p) throw() { return p; }
. The matching delete
operator is thus mandated to do nothing: void operator delete (void *, void *) throw() { }
. Usage:
void * p = ::operator new(5); // allocate only!
T * q = new (p) T(); // construct
q->~T(); // deconstruct: YOUR responsibility
// delete (p) q; <-- does not exist!! It would invoke the following line:
::operator delete(p, q); // does nothing!
::operator delete(q); // deallocate
The correct method is:
buf->~Buffer();
::operator delete(mem);
You can only delete with the delete
operator what you received from the new
operator. If you directly call the operator new
function, you must also directly call the operator delete
function, and must manually call the destructor as well.