What is the use of delete operator in c++?

Showing Answers 1 - 6 of 6 Answers

ryan

  • Sep 5th, 2011
 

To free some memory allocation, usually used with pointers.

  Was this answer useful?  Yes

Delete = free + destructor

So, If u want to free dynamically allocated memory, u can simply use free.
But if u want to call destructor as well u will have to use delete.

Lets take an example :

Code
  1. span style="color: #ff0000;">"mehul""mehul");

  2.  

  3.         delete (obj);

  4. }




Now, In this case, i want to call destructor of A to free the memory.
So, I will have to use delete.

Amit

  • Jan 6th, 2012
 

Delete: is used to deallocate memory from the heap that is created by new.

When you call delete, it firstly call the destructor of that object and then deallocate memory from heap.

class A
{

};

int
main()
{
A *a = new A();
delete a;

A *arr = new A[10];
delete []arr;
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions