Search code examples
c++staticlocaldestructornew-operator

is Destructor really destroy memory or just runs before the lifetime of object is going to end?


there are few question like this but not exactly same so please care to read whole question before cast it as duplicate one

1. objects created using new keyword (dynamic objects).

we explicitly use delete keyword to de-allocate there memory. so when program encounter delete keyword it get sense that object is going to end so it runs there destructor. is destructor de-allocate there memory ? I don't think so, as if we created destructor manually and do not code anything, just empty destructor still de-allocation is happen. means I feel destructor runs and then control get back to delete keyword and then memory de-allocation done.

if I am wrong please correct me.

if I am right then what's about if I call destructor manually for these dynamic objects with help of pointer p->~destructor()(where p is ptr pointing to these dynamic memory) it will de-allocate memory or not ? if not then while delete command it not goes into undefined behavior UB right ? if yes but how is going to de-allocate memory as destructor do not de-allocate memory and it has not delete command also. (I know when it reach delete it goes into UB)

2. Local objects

when there scope is going to end there memory destroyed. so when program encounter end of scope it get sense that particular object is going to end so it runs it's destructor and after that control back to called place(just before ending of scope) and then variable get out of scope and memory destroyed.

if I am wrong please correct me.

if I am right then what's about if I call destructor manually for this local object a. ~destructor (where a is local object) it will destroy memory or not ? if not while scope is going to end destructor invokes, then it not goes into UB right. if yes then how it is going to destroyed memory. as destructor do not de-allocate memory and it did not reach the end of scope also.

3. static objects

I think only difference between static and local objects is static destroyed while program is going to end and local destroyed when scope is going to end, so what is applicable for local objects regarding destructor, destruction of object is also applicable to static objects

4. if destructor do not de-allocate memory then what actually it does ?

my teacher says that destructor just give a moment to tell that these objects are going to destroyed so we can finish some stuff regarding these objects, is he right ? Also please tell extra points what destructor does.


Solution

  • or just runs before the lifetime of object is going to end?

    To be clear: The lifetime of the object ending and the calling of the destructor happen in the same point in time, not one after the other.

    is destructor de-allocate there memory ?

    Not necessarily. See following example for details.

    1. if destructor do not de-allocate memory then what actually it does ?

    Let's consider following class:

    struct example : base {
        another_class member;
        ~example() {
            // destructor body
        }
    };
    

    The destructor of example first executes the "destructor body", then it executes the destructors of all sub objects (which consists of member and base in this example). If the body, or the destructor of any sub object deallocates some memory, then that's what the destructor does. But the memory of the object being destroyed is generally not deallocated by the destructor.

    Indeed, delete does two things: Call the destructor, and dealloate the storage.

    what's about if I call destructor manually for these dynamic objects with help of pointer p->~destructor()(where p is ptr pointing to these dynamic memory)

    That destroys the object.

    it will destroy memory or not ?

    The storage is not deallocated by an explicit call to a destructor.

    then what's about if I call destructor manually for this local object a. ~destructor (where a is local object) it will destroy memory or not ?

    The storage is not deallocated by an explicit call to a destructor regardless of where the object is stored.

    if not while scope is going to end destructor invokes, then it not goes into UB right.

    If a destroyed object in automatic storage goes out of scope, then the behaviour of the program is undefined.