Reputation: 105
Take this code for example:
class MyClass
{
public:
~MyClass()
{
cout << "Destructor called\n";
}
};
int main()
{
MyClass Testvar;
// destructer called for this
MyClass *ptrvar;
ptrvar = &Testvar;
// but not for this
}
It brings a lots of confusions to me. The code above prints: Destructor called only once. I declared two MyClass instances inside main, one of them is a normal variable of type MyClass, and other is a pointer of same type pointing to the normal variable. There is no need of destructor here (no dynamic allocations) but I defined one in the class for a sake of example. So, because two class instances are defined, the destructor should be called twice. But that doesn't happened when I run this code. If I remove the pointer and define one more normal instance, the program prints:
Destructor called Destructor called
My observation is that destructors are not implicitly called when a pointer instance goes out of scope. Am I right or just missing something.
Upvotes: 0
Views: 742
Reputation: 1993
You only actually instantiated one object of type MyClass
. That happened in this line:
MyClass Testvar;
In the following line, you only declared a pointer to an object of type MyClass
, but this does not create a new object:
MyClass *ptrvar;
And in this line you assigned the address of your first MyClass
to your pointer:
ptrvar = &Testvar;
So the pointer is addressing the very same object, you still have only one instance of MyClass
. When the scope closes, TestVar
is deleted, and you see the destructor called once.
You could have created a new MyClass
object (on the heap) and assigned its address to your pointer like this:
MyClass *ptrvar = new MyClass();
Now you really do have two MyClass
objects. However, when the scope closes you'll still see only one object being deleted. This is because new
creates an object on the heap rather than on the stack, and such objects are not automatically deleted at the end of the scope in which they are created. You have to do this manually using delete
:
delete ptrvar;
When this line executes, you'll see your destructor is called. If you don't do this you've left your object on the heap, and have "leaked" the memory it occupies.
To save having to do all of this manually, you should make use of the in-built smart pointers that C++ provides.
Upvotes: 2
Reputation: 4411
I declared two MyClass instances inside main
No you didn't. You declared an instance of MyClass
and you created a pointer to that instance. That's all.
The behavior of your code is correct.
My observation is that destructors are not implicitly called when a pointer instance goes out of scope. Am I right or just missing something.
That's right. C++ does not provide a garbage collector. You have to keep track of your pointers by yourself. You can use the smart pointers to do so.
Upvotes: 5
Reputation: 317
You have created only one object and only for that object the destructor is called.
The pointer do not instantiate another object, it simply points to the previous one
Destructors are called when an object allocated on the stack goes out of scope and when object dynamically created (with operator new) are explicitly destroyed (operator delete)
Upvotes: 4