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));
?
It is also called general purpose pointer. It is not safe to delete a void pointer in C/C++ because delete needs to call the destructor of whatever object it's destroying, and it is impossible to do that if it doesn't know the type.
delete is used for one single pointer and delete[] is used for deleting an array through a pointer. This might help you to understand better.
The delete operator removes a given property from an object. On successful deletion, it will return true , else false will be returned.
When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor). If the operand to the delete operator is a modifiable l-value, its value is undefined after the object is deleted.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With