1

I was initiating new object on same local variable multiple times. From my test it seems I have some misunderstanding regarding destructor calling sequence.

So, here is the test code. I am initiating object of type A 3 times on variable obj. I was curious if destructor will be called as obj is not going out of scope. My idea was it will be called and another constructor will be called. So, the sequence will be like this

1st init constructor-> 1st initiated object destructor -> 2nd init constructor-> 2nd initiated object destructor -> 3rd init constructor-> 3rd initiated object destructor

But it seems this is not the case. I have printed the memory location of created/destroyed object when calling constructor and destructor. Also it seems multiple init creates object on stack without reference.

So, here is my code,

#include <iostream>

class A{
public:
        A()
        {
                std::cout<<"constructing object at "<<this<<std::endl;
        }

        ~A()
        {
                std::cout<<"destructing object at "<<this<<std::endl;
        }
};

int main()
{
        A obj = A();
        obj = A();
        obj = A();

        return 0;
}

Here is one output. Other runs have produced similar result with different pointer value.

constructing object at 0x7ffed4e26465
constructing object at 0x7ffed4e26466
destructing object at 0x7ffed4e26466
constructing object at 0x7ffed4e26467
destructing object at 0x7ffed4e26467
destructing object at 0x7ffed4e26465

So, here are my questions.

  1. local object variable resides on stack. Am I correct?
  2. Why 1st init object is destroyed at last but others created and destroyed sequentially. Is there any error in my findings?
  3. If in (2) answer is there is error, what is the sequence of constructor and destructor calling in case of multiple initialization of same variable?

I am using g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0

  • 1
    The last two are assignments after construction of the temporary. There is no need to destroy the original `A` object to do this. – PaulMcKenzie Jul 25 '19 at 18:49
  • TL;DR of the dupe: `A obj = A();` is converted to `A obj{};` thanks to copy elision. The other cases can't do that because they are assignment, not initialization. – NathanOliver Jul 25 '19 at 18:50
  • This is not a duplicate of that question: this is an incorrect supposition that C++ is like Java or Python (but with prompt destruction). – Davis Herring Jul 25 '19 at 18:59
  • @PaulMcKenzie, in that case I am losing the reference to the original object but it's still in stack? – Jaiaid Mobin Jul 26 '19 at 04:13
  • @JaiaidMobin -- Where is the first object being destroyed in your code? You are *assigning* values to the existing object -- that's what assignment means. This: `SomeObject a; SomeObject b; a = b;` does *not* "destroy" `a` when `a = b;` is invoked. – PaulMcKenzie Jul 26 '19 at 13:41
  • @PaulMcKenzie Ok, now I understand. The second and third are just creating temp objects and the values are assigned to original. Then temp object is destroyed hence different address than original and finally when original objects scope end it's being destroyed. Temp object creation was the part I didn't know. Thank you. – Jaiaid Mobin Jul 26 '19 at 19:04

0 Answers0