By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,419 Members | 1,124 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,419 IT Pros & Developers. It's quick & easy.

Missing Destructor

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a

"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

P: n/a

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

P: n/a
* 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

P: n/a
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

P: n/a
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

P: n/a
* 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

P: n/a
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

P: n/a

"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

P: n/a
* 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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.