473,503 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

destructors for static and non static pointer-to-objects

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
Jul 19 '05 #1
4 7521

"gorda" <sm*******@excite.com> wrote in message news:87**************************@posting.google.c om...

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.
Jul 19 '05 #2
gorda wrote:
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.

--
Unforgiven

"Most people make generalisations"
Freek de Jonge

Jul 19 '05 #3

"gorda" <sm*******@excite.com> wrote in message
news:87**************************@posting.google.c om...
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=&...=3f56491e%240%
2423178%249a6e19ea%40news.newshosting.com&prev=/groups%3Fhl%3Den%26lr%3D%26i
e%3DUTF-8%26group%3Dcomp.lang.c%252B%252B

HTH.
Jul 19 '05 #4
gorda wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2155
by: Laurens | last post by:
Hi, My C++ is pretty rusty after years of using Java, I can't recall the exact details of how destructors work. I know that a class's destructor is automagically called when an instance...
3
2138
by: Christopher Benson-Manica | last post by:
Okay, having purchased the excellent Josuttis book last night, I've almost got my little streambuf subclass bit worked out. Now, though, I'm faced with some issues with the destructors... ...
7
1875
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
2
3981
by: Brandon | last post by:
In a templated class I wrote, there is a public typedef for a function pointer. The class has an instance of the function pointer as a private member. That pointer is declared as static so I need...
7
3100
by: joevandyk | last post by:
Below, I have a class Container that contains a vector. The vector contains pointers to a Base class. When the Container destructor is called, I would like to delete all the objects that the...
15
2093
by: christopher diggins | last post by:
I posted to my blog a special pointer class work-around for inheriting from base classes without virtual destructors. I was wondering if there is any other similar work, and whether there are any...
4
2975
by: codymanix | last post by:
since structs are value types, is a destructor of a struct immediately called when the struct goes out of scope? this is definitely not the case with class-objects since they are allocated...
15
3463
by: Philipp | last post by:
Hello I don't exactly understand why there are no static virtual functions. I would have liked something like this: class Base{ static virtual std::string getName(){ return "Base"; } }
12
2853
by: Avalon1178 | last post by:
Hello, Are default destructors virtual? In other words, say I have "class A" and "class B : public A", and I have the code below: A * a = new A; B * b = new B; a = b;
9
1492
by: gabest | last post by:
I've got the following situation, simplified example of course. The thread would be deep behind interfaces, classes, etc. Is there any way to automagically exit it without having to add an Exit()...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.