473,747 Members | 2,927 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Missing Destructor

AB
Hi All,

I've got an array of objects, during the execution of the program I'd
like to assign a particular object to a certain element in the object
array. The sample code's like this...

class ClassA
{
public:
ClassA()
: m(rand() % 173) { cout<<"\nRandom Constructor: "<<m ; }

ClassA(int val)
: m(val) { cout<<"\nConstr ucted: "<<m ; }

~ClassA() { cout<<"\n\nDest ructor: "<<m ; }
private:
int m ;
} ;

int main()
{
srand((unsigned )time(NULL)) ;

const int size = 3 ;
ClassA *c = new ClassA[size] ;

c[0] = ClassA(200) ;
c[0] = ClassA(201) ;

delete[] c ;
return 0 ;
}

//Output...
Random Constructor: 50 <--- (object vanishes without destructor call)
Random Constructor: 38
Random Constructor: 148
Constructed: 200

Destructor: 200
Constructed: 201

Destructor: 201 <--- (1. 1st destructor call for this object)

Destructor: 148

Destructor: 38

Destructor: 201 <--- (2. object is being destroyed twice or has it
replaced the very first object?)

As I've pointed out in the output, the the destructor for the 1st
object never gets called, whereas the destructor for the object created
with data = 201 gets called twice. Is this the case or is it the very
first object which is in fact getting destroyed in (2) ?

Also, what's the best practice for such a situation?

Jun 29 '06 #1
11 1990

"AB" <ab*****@gmail. com> wrote in message
news:11******** *************@i 40g2000cwc.goog legroups.com...
Hi All,

I've got an array of objects, during the execution of the program I'd
like to assign a particular object to a certain element in the object
array. The sample code's like this...

class ClassA
{
public:
ClassA()
: m(rand() % 173) { cout<<"\nRandom Constructor: "<<m ; }

ClassA(int val)
: m(val) { cout<<"\nConstr ucted: "<<m ; }

~ClassA() { cout<<"\n\nDest ructor: "<<m ; }
private:
int m ;
} ;

int main()
{
srand((unsigned )time(NULL)) ;

const int size = 3 ;
ClassA *c = new ClassA[size] ;

c[0] = ClassA(200) ;
c[0] = ClassA(201) ;

delete[] c ;
return 0 ;
}

//Output...
Random Constructor: 50 <--- (object vanishes without destructor call)
Random Constructor: 38
Random Constructor: 148
Constructed: 200

Destructor: 200
Constructed: 201

Destructor: 201 <--- (1. 1st destructor call for this object)

Destructor: 148

Destructor: 38

Destructor: 201 <--- (2. object is being destroyed twice or has it
replaced the very first object?)

As I've pointed out in the output, the the destructor for the 1st
object never gets called, whereas the destructor for the object created
with data = 201 gets called twice. Is this the case or is it the very
first object which is in fact getting destroyed in (2) ?

Also, what's the best practice for such a situation?


All I can say is I see the same behavior in MS VC++ .net 2003


Jun 29 '06 #2

AB wrote:
Hi All,

I've got an array of objects, during the execution of the program I'd
like to assign a particular object to a certain element in the object
array. The sample code's like this...

class ClassA
{
public:
ClassA()
: m(rand() % 173) { cout<<"\nRandom Constructor: "<<m ; }

ClassA(int val)
: m(val) { cout<<"\nConstr ucted: "<<m ; }

~ClassA() { cout<<"\n\nDest ructor: "<<m ; }
private:
int m ;
} ;

int main()
{
srand((unsigned )time(NULL)) ;

const int size = 3 ;
ClassA *c = new ClassA[size] ;

c[0] = ClassA(200) ;
c[0] = ClassA(201) ;

delete[] c ;
return 0 ;
}

//Output...
Random Constructor: 50 <--- (object vanishes without destructor call)
Random Constructor: 38
Random Constructor: 148
Constructed: 200

Destructor: 200
Constructed: 201

Destructor: 201 <--- (1. 1st destructor call for this object)

Destructor: 148

Destructor: 38

Destructor: 201 <--- (2. object is being destroyed twice or has it
replaced the very first object?)

As I've pointed out in the output, the the destructor for the 1st
object never gets called, whereas the destructor for the object created
with data = 201 gets called twice. Is this the case or is it the very
first object which is in fact getting destroyed in (2) ?

Also, what's the best practice for such a situation?


A statement like
c[0] = ClassA(200) ;
constructs a temporary object and assigns it to c[0]. After the
statement the temporary object is destructed and not c[0]. That is the
reason you see contructed and destructed for 200. They refer to temp
object rather than c[0].

Jun 29 '06 #3
* AB:
Hi All,

I've got an array of objects, during the execution of the program I'd
like to assign a particular object to a certain element in the object
array. The sample code's like this...

class ClassA
{
public:
ClassA()
: m(rand() % 173) { cout<<"\nRandom Constructor: "<<m ; }

ClassA(int val)
: m(val) { cout<<"\nConstr ucted: "<<m ; }

~ClassA() { cout<<"\n\nDest ructor: "<<m ; }
private:
int m ;
} ;

int main()
{
srand((unsigned )time(NULL)) ;

const int size = 3 ;
ClassA *c = new ClassA[size] ;

c[0] = ClassA(200) ;
c[0] = ClassA(201) ;

delete[] c ;
return 0 ;
}

//Output...
Random Constructor: 50 <--- (object vanishes without destructor call)
Random Constructor: 38
Random Constructor: 148
Constructed: 200

Destructor: 200
Constructed: 201

Destructor: 201 <--- (1. 1st destructor call for this object)

Destructor: 148

Destructor: 38

Destructor: 201 <--- (2. object is being destroyed twice or has it
replaced the very first object?)

As I've pointed out in the output, the the destructor for the 1st
object never gets called, whereas the destructor for the object created
with data = 201 gets called twice. Is this the case or is it the very
first object which is in fact getting destroyed in (2) ?

Also, what's the best practice for such a situation?


What I call the "C++ construction guarantee": except for use of very
low-level features each object receives exactly one constructor call
(after all subobjects constructed), which is the first thing that
happens for that object, and for each constructor call there is at most
one destructor call -- unless you fail to destroy an object there is
exactly one destructor call for each constructor call.

The code above uses two techniques to make it less apparent what's going on:

* Value assignment that changes the apparent identity of an object.

* Temporary objects.

You first destructor call reporting id 201 is of the first array
element, the second is of the temporary object, which has the same id.

To investigate constructor and destructor calls you need code that is
easy to analyze instead of code that brings in all kinds of effects.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 29 '06 #4
AB
>Alf P. Steinbach wrote:
You first destructor call reporting id 201 is of the first array
element, the second is of the temporary object, which has the same id.


I thought it was the other way around. First the temporary object with
id 201 would be destroyed and the array's first object with id 201 last.

Jun 29 '06 #5
AB wrote:
Alf P. Steinbach wrote:
You first destructor call reporting id 201 is of the first array
element, the second is of the temporary object, which has the same id.


I thought it was the other way around. First the temporary object with
id 201 would be destroyed and the array's first object with id 201 last.


I'd say you're right. The temporary is destroyed as soon as the assignment
is finished. The array is destroyed later.

Jun 29 '06 #6
* Rolf Magnus:
AB wrote:
Alf P. Steinbach wrote:
You first destructor call reporting id 201 is of the first array
element, the second is of the temporary object, which has the same id.

I thought it was the other way around. First the temporary object with
id 201 would be destroyed and the array's first object with id 201 last.


I'd say you're right. The temporary is destroyed as soon as the assignment
is finished. The array is destroyed later.


Yes, that seems to be the case here.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 29 '06 #7
On 28 Jun 2006 22:00:14 -0700, "AB" <ab*****@gmail. com> wrote:
Hi All,

I've got an array of objects, during the execution of the program I'd
like to assign a particular object to a certain element in the object
array. The sample code's like this...

<....>

You may see everything more clearly if you add a copy assignment
operator:

class ClassA {
/* All your functions here */
ClassA& operator=(const ClassA& anotherInstance ) {
cout<<"\nCopy Assigned, changes from "<<m
<<" to "<<anotherInsta nce.m;
m=anotherInstan ce.m;
}
};

"One image is better than 1000 words"

Regards,

Zara
Jun 29 '06 #8

"Alf P. Steinbach" <al***@start.no > wrote in message
news:4g******** *****@individua l.net...
* Rolf Magnus:
AB wrote:
Alf P. Steinbach wrote:
You first destructor call reporting id 201 is of the first array
element, the second is of the temporary object, which has the same id.
I thought it was the other way around. First the temporary object with
id 201 would be destroyed and the array's first object with id 201 last.


I'd say you're right. The temporary is destroyed as soon as the
assignment
is finished. The array is destroyed later.


Yes, that seems to be the case here.


The question remains however. Why do we not see a Drestructor for 50?

//Output...
Random Constructor: 50 <--- (object vanishes without destructor call)
Random Constructor: 38
Random Constructor: 148
Constructed: 200

Destructor: 200
Constructed: 201

Destructor: 201 <--- (1. 1st destructor call for this object)

Destructor: 148

Destructor: 38

Destructor: 201 <--- (2. object is being destroyed twice or has it
replaced the very first object?)
Jun 29 '06 #9
* Jim Langston:

The question remains however. Why do we not see a Drestructor for 50?

Quoting myself:
Value assignment that changes the apparent identity of an object.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 29 '06 #10

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

Similar topics

52
27023
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object will call it's destructor when it is made a target of a delete".
11
10522
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor" is called in Case 2 since only ~base() becomes "virtual" and ~Derived() is still non-virtual? 3. Does Case 3 show that we don't need any virtual destructor to make ~Derived() called? 4. Is "virtual destructor" needed only for Case 2?
15
2117
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 code: template<typename target_type> class base_class_ptr {
37
4175
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
11
5311
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our 4-month old C#/MC++ .NET project project. I'd like to thank in advance anyone who takes the time to read and/or respond to this message. At a couple points, it may seem like a rant against C# / .NET, but we are pretty firmly stuck with this approach...
35
3322
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a member. However, I set a breakpoint at the destructor of this instance's class and it was never called!!! I can see how it might not get called at a deterministic time. But NEVER? So, I guess I need to know the rules about destructors. I would...
14
3208
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
23
2598
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624. I read the description, decided that it's exactly what I want, and ignored the warning. Now I'm trying to inherit using a template. Instead of "destructor could not be generated because a base class destructor is inaccessible", I now have an...
8
2056
by: gw7rib | last post by:
I've been bitten twice now by the same bug, and so I thought I would draw it to people's attention to try to save others the problems I've had. The bug arises when you copy code from a destructor to use elsewhere. For example, suppose you have a class Note. This class stores some text, as a linked list of lines of text. The destructor runs as follows: Note::~Note() {
0
8979
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
8818
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
9522
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
9223
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
6790
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
4588
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
4860
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3296
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
3
2203
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.