slah Omar
slah Omar

Reputation: 55

The difference between a destructor and 'delete'

What is the difference between the destruction of an object and deletion in C++? In OOP, when an object goes out of the scope, the destructor is called, and when dynamically allocating memory we have to use the delete keyword inside the destructor. So what does it mean for an object to be destroyed or deleted?

Upvotes: 2

Views: 3739

Answers (4)

Asteroids With Wings
Asteroids With Wings

Reputation: 17454

In the same way that a constructor is only part of the process of creating an object, invoking the destructor is just part of the process of destroying an object. It is a place where special clean-up code can go, like closing file handles or destroying other, indirectly-owned objects.

delete is the way you trigger the deletion of a dynamically allocated object. Other types of object are deleted automatically when they go out of scope. This keyword will clean up memory and more abstractly end the object's lifetime so that the compiler knows it doesn't conceptually "exist" any more.

You generally don't call the destructor manually, as it is usually insufficient for safely ending the lifetime of an object on its own. However, doing so is useful when you're taking super-fine-grained control over objects' existences, with (for example) placement new: then, it can be sufficient. But this is a special case.

Upvotes: 0

AAEM
AAEM

Reputation: 1849

There are multiple ways for memory allocation some of them are:

  1. Automatic memory allocation :
    In this case ,the variables defined inside a scope ,for example functions, are automatic allocated, and is usually stored on the stack. You have a limited control over the lifetime of this memory. E.g: automatic variables in a function are only there until the function finishes, then when going out of that scope, their destructors are called automatically i.e. you must not call delete to free the memory reserved for that object.
  2. Dynamic memory allocation :
    In this case , the variables defined inside a scope ,for example functions, are dynamically allocated. You control the size and the lifetime of these memory locations. If you don't free the reserved memory (delete it), the allocated memory is still valid and accessible, even though the function terminated (went out of scope) and in this case you'll have memory leakage, which may cause the program to crash. So the object should be delete explicitly which in turn will call the destructor for that object.

Upvotes: 0

Mikael H
Mikael H

Reputation: 1383

A destructor is a special method for a class. It is called whenever an object goes out of scope, or someone calls it explicitly.

An example where an objects destructor is called, but no delete:

struct MyObject
{
    MyObject() { std::cout << "default constructor"; }
    ~MyObject() { std::cout << "destructor"; }
    void doWork() {};
}

int main()
{
    {
        MyObject object; //constructor is called and memory allocated for the object on the stack
        object.doWork();
    } //at the end of scope, the destructor of MyObject is called, and the stack memory is "released"
    // object is not longer available and stack memory is free
}

An example when scope does not help you:

int main()
{
    {
        MyObject* object_ptr = new MyObject; //constructor is called and memory allocated on the heap. The pointer_ptr object 
        object_ptr->doWork();
    } //at the end of scope the object_ptr is removed from the stack, but the destructor for the MyObject is not called!
    // object_ptr is no longer available.
}

For the above example, we have an issue with the dynamically allocated memory. We explicitly call the destructor of the object we are pointing to by calling delete (and freeing the memory where MyObject is residing!):

int main()
{
    {
        MyObject* object_ptr = new MyObject; //constructor is called and memory allocated on the heap. The pointer_ptr object 
        object_ptr->doWork();
        delete object_ptr; // destructor of MyObject is called and memory freed.
        // object_ptr is still usable here, but it is pointing at an destroyed object, so don't use it! (unless you redirect it)
    } 
}

So delete is for managing the dynamic memory, but the destructor is a method of the class itself, which is always called when the object is getting freed from the memory (stack or heap). The destructor is called when freeing the memory for the object, so that if the object itself handles memory, is has time to release that memory. That's why smart pointers are good:

int main()
{
    {
        std::unique_ptr<MyObject> object_ptr = std::make_unique<MyObject>(); //constructor is called and memory allocated on the heap. The pointer_ptr object 
        object_ptr->doWork();
    }  //destructor for std::unique_ptr<MyObject> is called, which in turn calls delete on the object it is pointing at, which calls the destructor of that object!
}

Upvotes: 4

john
john

Reputation: 87944

When an object is destroyed it's destructor is called. A pointer to a dynamically allocated memory can be deleted, objects themselves can't be deleted (unless you think of a pointer as a kind of object).

Either of these actions can have knock on effects that cause the other action to happen (or more instances of the same action). For instance if you delete a pointer to an object, then the objects destructor will be called (if it has one) and it's destructor may cause other pointers to be deleted and other objects to be destroyed.

Basically destructor is about the death of objects and deletion is about the freeing of dynamically allocated memory, but the two actions are often intertwined.

Upvotes: -1

Related Questions