Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why is the Constructor called 4 times but the Destructor 5 times?

Reply
Thread Tools

Why is the Constructor called 4 times but the Destructor 5 times?

 
 
djskrill
Guest
Posts: n/a
 
      09-30-2003
Why is the Constructor called 4 times but the Destructor 5 times? I am
using MS VC 6. If this has been covered already please let me know.

The Code:

#include <stdio.h>

class MyClass {
private:
int x;
int y;
static int count;
int id;
public:
MyClass(int x=0,int y=0): x(x),y(y){
printf("Create (%d)\n",++count);
id = count;
}
~MyClass(){
printf("kill (%d)\n",id);
}
MyClass(const MyClass &r){
printf("Copy\n");
x = r.x;
y = r.y;
}

void Set(int ix,int iy){
x = ix; y = iy;
}
void Print(){
printf("x=%d y=%d\n",x,y);
}

//operators
MyClass operator + (const MyClass &left){
printf("Start +\n");
MyClass temp;
temp.x = x + left.x; temp.y = y + left.y;
printf("End +\n");
return temp;
}
MyClass &operator += (const MyClass &left){
x += left.x; y += left.y;
return *this;
}
MyClass &operator += (int left){
x += left; y += left;
return *this;
}
MyClass &operator = (const MyClass &r){
x = r.x; y = r.y;
return *this;
}
};

int MyClass::count = 0;

int main(int argc,char **argv){

MyClass c1(5,6);
MyClass c2(1,1);
MyClass c3(100,200);



printf("before\n");
c1.Print();
c2.Print();
c3.Print();

c3 = c1+ c2;

printf("after\n");
c1.Print();
c2.Print();
c3.Print();


return 0;
}
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      09-30-2003

"djskrill" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) om...
> Why is the Constructor called 4 times but the Destructor 5 times? I am
> using MS VC 6. If this has been covered already please let me know.


A constructor is called 5 times. After modifying your copy constructor to actually
initialize the id field:

MyClass& operator=(const MyClass& r) {
id = r. id;
x = r.x;
y = r.y;
}

This is what I get as output: (annoated)
Create (1) // main c1 variable
Create (2) // main c2 variable
Create (3) // main c3 variable
before
x=5 y=6
x=1 y=1
x=100 y=200
Start +
Create (4) // operator+ temp
End +
Copy (5) // return value temporary
kill (4)
kill (5)
after
x=5 y=6
x=1 y=1
x=6 y=7
kill (3)
kill (2)
kill (1)


Which



 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      09-30-2003

"djskrill" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Why is the Constructor called 4 times but the Destructor 5 times? I am
> using MS VC 6. If this has been covered already please let me know.
>
> The Code:


Please indent your code. It's not very readable without it.
>
> #include <stdio.h>


Why aren't you using iostreams?

>
> class MyClass {
> private:
> int x;
> int y;
> static int count;
> int id;
> public:
> MyClass(int x=0,int y=0): x(x),y(y){
> printf("Create (%d)\n",++count);
> id = count;
> }
> ~MyClass(){
> printf("kill (%d)\n",id);
> }



> MyClass(const MyClass &r){


This is a constructor. A 'copy constructor'.

> printf("Copy\n");
> x = r.x;
> y = r.y;


You've left 'count' uninitialized.
Evalutating its value will give undefined behavior.

> }


Also, why aren't you using initializer list?

MyClass(const MyClass &r) : x(r.x), y(r.y), count(r.count)
{
printf("Copy\n");
}

More below.

>
> void Set(int ix,int iy){
> x = ix; y = iy;
> }
> void Print(){
> printf("x=%d y=%d\n",x,y);
> }
>
> //operators
> MyClass operator + (const MyClass &left){
> printf("Start +\n");
> MyClass temp;
> temp.x = x + left.x; temp.y = y + left.y;
> printf("End +\n");
> return temp;
> }
> MyClass &operator += (const MyClass &left){
> x += left.x; y += left.y;
> return *this;
> }
> MyClass &operator += (int left){
> x += left; y += left;
> return *this;
> }
> MyClass &operator = (const MyClass &r){
> x = r.x; y = r.y;
> return *this;
> }
> };
>
> int MyClass::count = 0;
>
> int main(int argc,char **argv){
>
> MyClass c1(5,6);
> MyClass c2(1,1);
> MyClass c3(100,200);
>
>
>
> printf("before\n");
> c1.Print();
> c2.Print();
> c3.Print();
>
> c3 = c1+ c2;
>
> printf("after\n");
> c1.Print();
> c2.Print();
> c3.Print();
>
>
> return 0;
> }


I get output with VC++6.0 SP5:

Create (1)
Create (2)
Create (3)
before
x=5 y=6
x=1 y=1
x=100 y=200
Start +
Create (4)
End +
Copy <<== note that this is a ctor call
kill (4)
kill (-858993460) <<== this alerted me to the 'uninitialized' problem
after
x=5 y=6
x=1 y=1
x=6 y=7
kill (3)
kill (2)
kill (1)


I see five ctor calls, and five dtor calls.

-Mike


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-30-2003

"Mike Wahler" <(E-Mail Removed)> wrote in message
news:%Vkeb.9124$(E-Mail Removed) ink.net...

> > MyClass(const MyClass &r){

>
> This is a constructor. A 'copy constructor'.
>


After reading Ron's reply, I realized I have erred.

> > printf("Copy\n");
> > x = r.x;
> > y = r.y;

>
> You've left 'count' uninitialized.


'id'


> Evalutating its value will give undefined behavior.
>
> > }

>
> Also, why aren't you using initializer list?
>
> MyClass(const MyClass &r) : x(r.x), y(r.y), count(r.count)


MyClass(const MyClass &r) : x(r.x), y(r.y), id(r.id)


Sorry about that. That's what I get for not compiling and
testing my corrections.

-Mike


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-30-2003

"Mike Wahler" <(E-Mail Removed)> wrote in message news:%Vkeb.9124$(E-Mail Removed) ink.net...

> Please indent your code. It's not very readable without it.


His code is indented. It's just that your news reader (presumably outlook express)
is eating the tabs on display.


>
> You've left 'count' uninitialized.
> Evalutating its value will give undefined behavior.


Actually, he left id uninitialized. Count is a static.


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-30-2003

"Mike Wahler" <(E-Mail Removed)> wrote in message news:2Zkeb.9127$(E-Mail Removed) ink.net...

>
> MyClass(const MyClass &r) : x(r.x), y(r.y), id(r.id)
>
>
> Sorry about that. That's what I get for not compiling and
> testing my corrections.
>

Actually, he doesn't want to copy the id. He wants to give it
a new serial number, otherwise it's impossible to pair up the destructors.


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-30-2003

"Ron Natalie" <(E-Mail Removed)> wrote in message
news:3f79e497$0$36960$(E-Mail Removed) m...
>
> "Mike Wahler" <(E-Mail Removed)> wrote in message

news:2Zkeb.9127$(E-Mail Removed) ink.net...
>
> >
> > MyClass(const MyClass &r) : x(r.x), y(r.y), id(r.id)
> >
> >
> > Sorry about that. That's what I get for not compiling and
> > testing my corrections.
> >

> Actually, he doesn't want to copy the id. He wants to give it
> a new serial number, otherwise it's impossible to pair up the destructors.


Oh, yes, overlooked that.

-Mike


 
Reply With Quote
 
Marcelo Pinto
Guest
Posts: n/a
 
      10-01-2003
"Mike Wahler" <(E-Mail Removed)> wrote in message news:<%Vkeb.9124$(E-Mail Removed) link.net>...
[snip]
> > MyClass(const MyClass &r){

>
> This is a constructor. A 'copy constructor'.
>
> > printf("Copy\n");
> > x = r.x;
> > y = r.y;

>
> You've left 'count' uninitialized.
> Evalutating its value will give undefined behavior.
>
> > }

>
> Also, why aren't you using initializer list?
>
> MyClass(const MyClass &r) : x(r.x), y(r.y), count(r.count)
> {
> printf("Copy\n");
> }
>

but count is static and need not be initialized in constructors. I
believe you meant id. But then again, the proper way would be:

MyClass(const MyClass &r) : x(r.x), y(r.y)
{
++count;
id = count;
printf("Copy create (%d)\n",++count);
}

which would correct two problems: the garbage you get when printing id
at destruction time, and accounting for the construction of the
object.

Regards,

Marcelo Pinto
 
Reply With Quote
 
Marcelo Pinto
Guest
Posts: n/a
 
      10-01-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Marcelo Pinto) wrote in message news:<(E-Mail Removed). com>...
> "Mike Wahler" <(E-Mail Removed)> wrote in message news:<%Vkeb.9124$(E-Mail Removed) link.net>...

[snip]
> but count is static and need not be initialized in constructors. I
> believe you meant id. But then again, the proper way would be:
>
> MyClass(const MyClass &r) : x(r.x), y(r.y)
> {
> ++count;
> id = count;
> printf("Copy create (%d)\n",++count);

[snip]
forgive me the ++count inside the printf, it should be:
printf("Copy create (%d)\n",count);

Regards,

Marcelo Pinto
 
Reply With Quote
 
djskrill
Guest
Posts: n/a
 
      10-01-2003
Thanks for the help.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 2 05-20-2008 02:20 AM
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 0 05-19-2008 07:05 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
After calling constructor, the destructor is called immediately - why? mlimber C++ 4 01-05-2006 07:57 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments