Alex Rivera | Logout

Delete vs operator delete (and void pointer)

Asked 2012-05-13T17:18:46.343
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));

?

Edit
Report

1 Answer

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.

answered 2012-05-13T17:43:13.353

Your Answer