c++destructornew-operatordelete-operator

Does a delete expression invoke the destructor?


I am allocating memory to a object dynamically and then if I call delete what happens? the destructor is called or delete function has a different way of handling memory?

Consider the following example:

class A 
{
 int x;
}

int main()
{
    A *ptr = new A();
    delete ptr;
    return 0;
}

where is the destructor called?


Solution

  • An expression with the delete operator, first calls the appropriate destructor (if needed), and then calls function operator delete to release the storage. Have a look here for details.