473,569 Members | 2,839 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 << "constructo r 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 7529

"gorda" <sm*******@exci te.com> wrote in message news:87******** *************** ***@posting.goo gle.com...

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*******@exci te.com> wrote in message
news:87******** *************** ***@posting.goo gle.com...
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%249a6e1 9ea%40news.news hosting.com&pre v=/groups%3Fhl%3De n%26lr%3D%26i
e%3DUTF-8%26group%3Dcom p.lang.c%252B%2 52B

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 << "constructo r 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
2156
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 goes out of scope(when it unwinds from the stack). However, I can't recall whether destructors of member fields get called automatically when this...
3
2142
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... public TRFileStream : public TLSFile, public std::streambuf { ... }; The problem, I think, is that ~TLSFile is *not* virtual, but
7
1880
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 destructors. What I am expecting is, if the destructor is declared as virtual in base, the destructors of all its derived classes also should be...
2
3986
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 to declare an instance of the pointer. So, I tried using the keyword typename to declare the static member. I don't get any syntax errors but...
7
3106
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 pointers in the vector are pointing to. It works fine, IF I comment out the destructors in Base and Inherited. If I leave the explicit destructors in...
15
2101
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 problems with my proposed approach. Thanks in advance! See http://www.artima.com/weblogs/viewpost.jsp?thread=107587 For those who just want the...
4
2979
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 dynamically are cleaned up by the gc at a undefined time. -- cody www.deutronium.de.vu || www.deutronium.tk
15
3474
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
2857
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
1493
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() call to everywhere? I just want to break that loop when the application is done (the thread is still running so it cannot be done, but I think there...
0
7922
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5218
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
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 we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.