473,395 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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<<"\nConstructed: "<<m ; }

~ClassA() { cout<<"\n\nDestructor: "<<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 1940

"AB" <ab*****@gmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.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<<"\nConstructed: "<<m ; }

~ClassA() { cout<<"\n\nDestructor: "<<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<<"\nConstructed: "<<m ; }

~ClassA() { cout<<"\n\nDestructor: "<<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<<"\nConstructed: "<<m ; }

~ClassA() { cout<<"\n\nDestructor: "<<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 "<<anotherInstance.m;
m=anotherInstance.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*************@individual.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
Jim Langston wrote:

"Alf P. Steinbach" <al***@start.no> wrote in message
news:4g*************@individual.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?


Because you copied the object with id 201 to it, so it now has id 201, too.
What did you expect to happen with the object when assigning another object
to it?

Jun 29 '06 #11
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:e8*************@news.t-online.com...
Jim Langston wrote:

"Alf P. Steinbach" <al***@start.no> wrote in message
news:4g*************@individual.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?


Because you copied the object with id 201 to it, so it now has id 201,
too.
What did you expect to happen with the object when assigning another
object
to it?


Wasn't me, but someone else. Yes, now I understand. No destructor for
assignment.
Jun 29 '06 #12

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

Similar topics

52
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...
11
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"...
15
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...
37
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
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...
35
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...
14
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
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....
8
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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,...
0
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
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...

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.