473,811 Members | 3,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When does a destructor run...

Which is a more correct statement?

A destructor runs automatically when an object is deallocated.

or

A destructor runs automatically immediately before an object is
deallocated.

Oct 19 '05 #1
7 2268
cgama...@gmail. com wrote:
Which is a more correct statement?

A destructor runs automatically when an object is deallocated.

or

A destructor runs automatically immediately before an object is
deallocated.


I don't see a contradiction between them. The second provides more
information, however. Compare this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-16.9

Cheers! --M

Oct 19 '05 #2
mlimber wrote:
cgama...@gmail. com wrote:
Which is a more correct statement?
First of all, you need to specify that you're talking about dynamic or
automatic objects. Statics may never have their storage deallocated.
A destructor runs automatically when an object is deallocated.

or

A destructor runs automatically immediately before an object is
deallocated .

I don't see a contradiction between them. The second provides more
information, however. Compare this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-16.9


The destructor can also be called explicitly. But regardless of that,
after the destructor has been called, there is no more "object", so to
be entirely semantically correct, the second statement is a bit wrong
saying "an object is deallocated". The storage is deallocated, not the
object.

V
Oct 19 '05 #3
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
<cg******@gmail .com> wrote:
Which is a more correct statement?

A destructor runs automatically when an object is deallocated.

or

A destructor runs automatically immediately before an object is
deallocated.


I don't like the choices, but if had to choice faced
with life or death, would go with choice 1.
That said, I'll bet that you probably have some other underlying
thought to ask?
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 19 '05 #4
It was a test question, which I got wrong. :) I want to argue the
point, and need some expert ammunition. :)

When I think "deallocate d" I'm talking about what happens to a
non-static object when it passes out of scope.

-=-

class MyThing
{
public:
MyThing();
~MyThing();

};

MyThing::MyThin g()
{
//blah blah
};

MyThing::~MyThi ng()
{
//destruct destruct
};

void Func()
{
MyThing t;
}

int main()
{

Func();
return 0;

}

-=-

Here how I imagine things happening: in Func, MyThing is "allocated"
and the constructor runs. When Func is over, MyThing passes out of
scope ... First the destructor runs, to take care of anything that the
programmer needs taken care of. The last thing to happen is
"deallocati on" of the space taken up by the object t. The thing that
happens right before "deallocati on" is the destructor running.

Maybe I don't understand the word "deallocate d" ... Maybe I don't
understand what happens when an object passes out of scope.

Oct 19 '05 #5

<cg******@gmail .com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
It was a test question, which I got wrong. :) I want to argue the
point, and need some expert ammunition. :)

When I think "deallocate d" I'm talking about what happens to a
non-static object when it passes out of scope.

-=-

class MyThing
{
public:
MyThing();
~MyThing();

};

MyThing::MyThin g()
{
//blah blah
};

MyThing::~MyThi ng()
{
//destruct destruct
};

void Func()
{
MyThing t;
}

int main()
{

Func();
return 0;

}

-=-

Here how I imagine things happening: in Func, MyThing is "allocated"
and the constructor runs. When Func is over, MyThing passes out of
scope ... First the destructor runs, to take care of anything that the
programmer needs taken care of. The last thing to happen is
"deallocati on" of the space taken up by the object t. The thing that
happens right before "deallocati on" is the destructor running.

Maybe I don't understand the word "deallocate d" ... Maybe I don't
understand what happens when an object passes out of scope.


You've got the right idea. But it's not the object itself (e.g., MyThing)
being "deallocate d", but rather the storage (memory) for it. But your
ordering is correct. The constructor is called after memory is allocated
for an object, and the destructor is called before that memory is released.

If you're going to argue the point with your instructor, then you need to
ask exactly what he meant by the phrase "object is deallocated". If he just
meant "when an object goes out of scope" or "when delete is called on a
pointer", then answer #1 would be correct enough, since that is when the
destructor gets called. But if he meant "when the storage for the object is
released", then answer #2 is more correct.

Basically, I'd say that the question is poorly worded in the first place.

-Howard

Oct 19 '05 #6
cg******@gmail. com wrote:
It was a test question, which I got wrong. :) I want to argue the
point, and need some expert ammunition. :)

When I think "deallocate d" I'm talking about what happens to a
non-static object when it passes out of scope.

-=-

class MyThing
{
public:
MyThing();
~MyThing();

};

MyThing::MyThin g()
{
//blah blah
};

MyThing::~MyThi ng()
{
//destruct destruct
};

void Func()
{
MyThing t;
}

int main()
{

Func();
return 0;

}

-=-

Here how I imagine things happening: in Func, MyThing is "allocated"
and the constructor runs. When Func is over, MyThing passes out of
scope ... First the destructor runs, to take care of anything that the
programmer needs taken care of. The last thing to happen is
"deallocati on" of the space taken up by the object t. The thing that
happens right before "deallocati on" is the destructor running.

Maybe I don't understand the word "deallocate d" ... Maybe I don't
understand what happens when an object passes out of scope.


Your summary is accurate. Deallocation -- whether from the stack for
automatic objects or for dynamically allocated objects from some other
pool of memory (like the one manipulated by malloc/free or one
associated with a custom allocator, e.g., for a system with segmented
memory) -- is the process of making a block of memory available for
reallocation. Destruction is simply the invocation of destructors,
which must happen before the memory in which the object resides is
deallocated.

In C++, when an automatic object goes out of scope (at the end of its
block or when an exception is thrown), the object is destructed and
then the memory in which it resided is deallocated. Of course, if the
object itself had any other memory for which it was responsible --
whether allocated in the constructor, member functions, or by direct,
external manipulation of its members -- that should also be destructed
and deallocated. std::auto_ptr and other such constructs help manage
this process.

Compare the FAQs on "placement new" for more what goes on behind the
scenes in object instantiation and destruction since essentially the
same thing happens for objects allocated on the stack like "t" in
Func() above:

http://www.parashift.com/c++-faq-lit....html#faq-11.9
http://www.parashift.com/c++-faq-lit...html#faq-11.10
http://www.parashift.com/c++-faq-lit...html#faq-11.14

Cheers! --M

Oct 19 '05 #7

"Greg Comeau" <co****@panix.c om> wrote in message
news:dj******** **@panix3.panix .com...
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
<cg******@gmail .com> wrote:
Which is a more correct statement?

A destructor runs automatically when an object is deallocated.

or

A destructor runs automatically immediately before an object is
deallocated .


I would say the second is more correct. ("immediatel y before an object is
deallocated")

My main reasoning here, is that the members of the object still exist, and
can be referenced within the destructor. After the destructor is called,
then the memory is de-allocated.
Oct 20 '05 #8

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

Similar topics

4
1741
by: Aaron W. LaFramboise | last post by:
I'm seeking a solution to my C++-life long dilemma of how to deal with errors when exceptions aren't appropriate. Below I highlight two error cases, which mainly occur when trying to handle "unexpected" errors on system interfaces. I am an experienced modern C++ programmer, and I'm mainly interested in the case where the error is in a generic, reusable component where invasive control over the user is unacceptable. Case 1: An error...
9
4389
by: Razzie | last post by:
Hey all, When should or should you not inherit from the IDisposable interface for your custom classes? Say I have a custom class SomeClass that does... some stuff. If I create a new instance of SomeClass and it goes out of scope, the GC will always clean that up, right? So, why should I undergo the trouble of inheriting from IDisposable, implement the Dispose() and Dispose(bool disposed) methods - when there is no difference? Are...
1
8122
by: Joannes Vermorel | last post by:
I am currently trying to port a small open source scientfic library written in C++ to .Net. The code (including the VS solution) could be found at http://www.vermorel.com/opensource/selfscaling.zip My problem is that when I try to compile the library I got a list of linking error messages. I am not a specialist of porting C++ code to .Net. Does anyone has an idea on how to make this code compile in .Net ? Thanks, Joannes Vermorel
9
6063
by: Simon | last post by:
Hi, I have written an ActiveX object to resize images and upload them to a database, this all works fine but when I close internet explorer the process iexporer.exe is still running in my task manager and I can not launch anything from the desktop (eg. a web shortcut) without firt killing this process. The object is launched using JScript with the following code: var Launcher = new ActiveXObject("LaunchControl");
22
2425
by: ypjofficial | last post by:
Hello All, I have following doubt.. class abstractclass { public: abstractclass(){} virtual void method()=0; };
10
2980
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The constructors and copy constructors in 'A' report when they are called. 'whoami' is just a unique identifier assigned to every object of type 'A'. The output of the program is: constructor 0 constructor 1
1
1676
by: Amit Dedhia | last post by:
Hi I am developing an application using C++/CLI in Visual Studio 2005. In my application I have following structure. (1) Class A creates instances of Class B and Class C. (2) Class B implements a timer function which repeatedly executes a timer every 250 ms. (3) Class C instance hooks for a event raised by Class B instance. Class B always raises this event asynchronously at the end of timer
3
1720
by: nick4ng | last post by:
If I have a templated constructor, there is a way to know, at compile time, the instance when compiling the destructor? To be clearer my problem is that I have a templated constructor that is empty for particular arguments. I need to have an empty destructor in this case as well. Is it possible? I should add that for other reasons I can not have a templated class.
6
2061
by: Pallav singh | last post by:
Hi All How Does compiler implement virtual destructor ??? as we all know if base class destructor is virtual..........then while wriiting statemnt like this Base * b = new Derived( ); delete b;
0
9731
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10393
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10136
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5556
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3871
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.