Delete vs operator delete (and void pointer)
Asked Answered
D

1

14

Does delete ptr differ from operator delete(ptr) only in this, that delete calls ptr destructor? Or in other words, does delete ptr first call a destructor of ptr and then operator delete(ptr) to free allocated memory? Then is delete ptr technically equivalent to the following:

T * ptr = new T;

//delete ptr equivalent:
ptr->~T();
::operator delete(static_cast<void *>(ptr));

?

Dissert answered 13/5, 2012 at 17:18 Comment(1)
Yes, that's the only difference, besides the fact that you can overload operator delete.Nelan
G
9

delete ptr will do overload resolution for operator delete, so it may not call the global ::operator delete

But otherwise, yes. The delete operator calls the relevant destructor, if any, and then calls operator delete.

Galumph answered 13/5, 2012 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.