Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which operator delete?

Tags:

c++

operators

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.

like image 469
There is nothing we can do Avatar asked Aug 27 '10 18:08

There is nothing we can do


People also ask

What is delete operator C++?

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.

How delete [] is different from delete?

delete is used for one single pointer and delete[] is used for deleting an array through a pointer.

What is the syntax of delete operator?

Syntax of delete operatordelete pointer_variable; // delete ptr; It deallocates memory for one element.

Is there a delete function in C++?

C++ remove()The remove() function in C++ deletes a specified file. It is defined in the cstdio header file.


1 Answers

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.

like image 171
Dima Avatar answered Oct 21 '22 18:10

Dima