destructors for static and non static pointer-to-objects

G

gorda

Hello,

I have the following code, with a basic class having a constructor and
destructor. in the main section, I create a static pointer to the
class. My question is regarding the destructor. If I dont explicitly
delete the pointer, the destructor is called. However, I expected that
even with out explicitly deleting it, the destructor would be called
when the program ends.

#include <iostream>
using namespace std;

class basic
{
public:
basic()
{ cout << "constructor called" << endl; }

~basic()
{ cout << "destructor" << endl; }
};

int main()
{
static basic *bp = new basic;
//delete bp;
}

Output without the explicit delete call:
constructor

Output with the explicit delete call:
constructor
destructor


Can someone tell me why the destructor is not called when the pointer
is not explicitly deleted?

Thanks,
-Gorda Smith
 
R

Ron Natalie

gorda said:
Can someone tell me why the destructor is not called when the pointer
is not explicitly deleted?

It's not supposed to. The fact that the pointer is destroyed doesn't mean
that the object it points to is. This is the way C++ is intended to work.
 
U

Unforgiven

gorda said:
Output without the explicit delete call:
constructor

Output with the explicit delete call:
constructor
destructor

A class is autodestructed only when it goes out of scope. Unfortunately,
only values that live on the stack or in the global data section of your
program will go out of scope; the former when the function they were created
in ends and the latter when the program ends.

In your example, the pointer lives on the stack, but your actual class
instance - as anything allocated by new - lives on the heap. The pointer
goes out of scope when main ends, but the class instance does not, hence the
destructor is called.

This is by design. Therefore, in C++, you *must* *always* have exactly as
many calls to delete as you do to new. Less calls to delete is a memory
leak.
 
K

Kevin Goodsell

gorda said:
Hello,

I have the following code, with a basic class having a constructor and
destructor. in the main section, I create a static pointer to the
class. My question is regarding the destructor. If I dont explicitly
delete the pointer, the destructor is called.

I doubt that, and your later statements contradict it.
However, I expected that
even with out explicitly deleting it, the destructor would be called
when the program ends.

Why would you expect that?
#include <iostream>
using namespace std;

class basic
{
public:
basic()
{ cout << "constructor called" << endl; }

~basic()
{ cout << "destructor" << endl; }
};

int main()
{
static basic *bp = new basic;
//delete bp;
}

Output without the explicit delete call:
constructor

Output with the explicit delete call:
constructor
destructor


Can someone tell me why the destructor is not called when the pointer
is not explicitly deleted?

Certainly, if you'll explain why you believe it should be. The behavior
you observe is the expected behavior (to all of us, at least).

-Kevin
 
J

Josephine Schafer

gorda said:
Hello,

I have the following code, with a basic class having a constructor and
destructor. in the main section, I create a static pointer to the
class. My question is regarding the destructor. If I dont explicitly
delete the pointer, the destructor is called. However, I expected that
even with out explicitly deleting it, the destructor would be called
when the program ends.

A recent thread with 53 replies discussing this issue in detail -

http://groups.google.com/groups?dq=&hl=en&lr=&ie=UTF-8&threadm=3f56491e$0%
2423178%249a6e19ea%40news.newshosting.com&prev=/groups%3Fhl%3Den%26lr%3D%26i
e%3DUTF-8%26group%3Dcomp.lang.c%252B%252B

HTH.
 
Joined
Mar 7, 2010
Messages
4
Reaction score
0
#include <iostream>
using namespace std;

class basic
{
public:
basic()
{
cout << "constructor called" << endl;
}

~basic()
{
cout << "destructor" << endl; }
};

int main()
{
//static basic *bp = new basic;
static basic bp;
//delete bp;
}


when we say static basic bp, destructor is called even though we did not call it explicitly. why? what is the difference?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top