Is there a difference between:
operator delete(some_pointer);
and
delete some_pointer;
and if so what is the difference and where one should use one and where the other version of this operator? Thanks.
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 is used for one single pointer and delete[] is used for deleting an array through a pointer.
Syntax of delete operatordelete pointer_variable; // delete ptr; It deallocates memory for one element.
C++ remove()The remove() function in C++ deletes a specified file. It is defined in the cstdio header file.
Ironically, the delete
operator and operator delete()
are not the same thing.
delete some_pointer;
calls the destructor of the object pointed to by some_pointer
, and then calls operator delete()
to free the memory.
You do not normally call operator delete()
directly, because if you do, the object's destructor will not be called, and you are likely to end up with memory leaks.
The only time you have to care about operator delete()
is when you want to do your own memory management by overriding operator new()
and operator delete()
.
To top it off, you should also be aware that delete
and delete []
are two different things.
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