C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Confusion about STL vector and memory management

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
exits funnel
Guest





PostPosted: Fri Jan 30, 2004 3:08 am    Post subject: Confusion about STL vector and memory management Reply with quote



Hello,

I have the following simple program:

//BEGIN CODE
#include <iostream>
#include <vector>
class Mouse
{
public:
Mouse( ) { cout << "Mouse( ) this = " << this << "n"; }
~Mouse( ) { cout << "~Mouse( ) this = " << this << "n"; }
};
void foo(Mouse* mptr);
int main( )
{
Mouse m;
foo(&m);
cout << "Returned from foon";
}
void foo(Mouse* mptr)
{
vector v.push_back(*mptr);
cout << "Leaving foo( )n";
}
//END CODE

When I run it I get the following output:

//BEGIN OUTPUT
Mouse( ) this = 0xbffff857
Leaving foo( )
~Mouse( ) this = 0x804b498
Returned from foo
~Mouse( ) this = 0xbffff857
//END OUTPUT

As you can see there are two dtor calls but only one ctor call. I know
that STL containers 'own' their elements though I'm not sure what
exactly this means though I have the vague notion that it means that
they are responsible for deleting them if they still exist when the
vector itself goes out of scope. I guess that accounts for the first
dtor call but which object is being destroyed? Apparently not the one I
added. If anyone could shed some light on this I'd really appreciate
it. Thanks in advance.

-exits

Back to top
Jonathan Turkanis
Guest





PostPosted: Fri Jan 30, 2004 3:29 am    Post subject: Re: Confusion about STL vector and memory management Reply with quote



"exits funnel" <exitsfunnel (AT) NOSPAMyahoo (DOT) com> wrote

Quote:
Hello,

I have the following simple program:

//BEGIN CODE
#include <iostream
#include class Mouse
{
public:
Mouse( ) { cout << "Mouse( ) this = " << this << "n"; }
~Mouse( ) { cout << "~Mouse( ) this = " << this << "n"; }
};
void foo(Mouse* mptr);
int main( )
{
Mouse m;
foo(&m);
cout << "Returned from foon";
}
void foo(Mouse* mptr)
{
vector v.push_back(*mptr);
cout << "Leaving foo( )n";
}
//END CODE

When I run it I get the following output:

//BEGIN OUTPUT
Mouse( ) this = 0xbffff857
Leaving foo( )
~Mouse( ) this = 0x804b498
Returned from foo
~Mouse( ) this = 0xbffff857
//END OUTPUT

As you can see there are two dtor calls but only one ctor call.

The second constructor call is to an implicitly defined copy
constructor. Try defining one yourself one and see what happens.

Mouse( const Mouse&) { cout << "Mouse( const Mouse&) this = " <<
this << "n"; }

Jonathan



Back to top
Daniel T.
Guest





PostPosted: Fri Jan 30, 2004 3:38 am    Post subject: Re: Confusion about STL vector and memory management Reply with quote



In article <bvcif4$qupil$1 (AT) ID-216073 (DOT) news.uni-berlin.de>,
"Jonathan Turkanis" <technews (AT) kangaroologic (DOT) com> wrote:

Quote:
"exits funnel" <exitsfunnel (AT) NOSPAMyahoo (DOT) com> wrote in message
news:4019CADD.7060306 (AT) NOSPAMyahoo (DOT) com...
Hello,

I have the following simple program:

//BEGIN CODE
#include <iostream
#include class Mouse
{
public:
Mouse( ) { cout << "Mouse( ) this = " << this << "n"; }
~Mouse( ) { cout << "~Mouse( ) this = " << this << "n"; }
};
void foo(Mouse* mptr);
int main( )
{
Mouse m;
foo(&m);
cout << "Returned from foon";
}
void foo(Mouse* mptr)
{
vector v.push_back(*mptr);
cout << "Leaving foo( )n";
}
//END CODE

When I run it I get the following output:

//BEGIN OUTPUT
Mouse( ) this = 0xbffff857
Leaving foo( )
~Mouse( ) this = 0x804b498
Returned from foo
~Mouse( ) this = 0xbffff857
//END OUTPUT

As you can see there are two dtor calls but only one ctor call.

The second constructor call is to an implicitly defined copy
constructor. Try defining one yourself one and see what happens.

Mouse( const Mouse&) { cout << "Mouse( const Mouse&) this = "
this << "n"; }

Or even more interesting:

Mouse( const Mouse& other ) {
cout << "this = " << this << " other = " << &other << endl;
}

Back to top
exits funnel
Guest





PostPosted: Fri Jan 30, 2004 2:59 pm    Post subject: Re: Confusion about STL vector and memory management Reply with quote



Daniel T. wrote:
Quote:
In article <bvcif4$qupil$1 (AT) ID-216073 (DOT) news.uni-berlin.de>,
"Jonathan Turkanis" <technews (AT) kangaroologic (DOT) com> wrote:


"exits funnel" <exitsfunnel (AT) NOSPAMyahoo (DOT) com> wrote in message
news:4019CADD.7060306 (AT) NOSPAMyahoo (DOT) com...

Hello,

I have the following simple program:

//BEGIN CODE
#include <iostream
#include class Mouse
{
public:
Mouse( ) { cout << "Mouse( ) this = " << this << "n"; }
~Mouse( ) { cout << "~Mouse( ) this = " << this << "n"; }
};
void foo(Mouse* mptr);
int main( )
{
Mouse m;
foo(&m);
cout << "Returned from foon";
}
void foo(Mouse* mptr)
{
vector v.push_back(*mptr);
cout << "Leaving foo( )n";
}
//END CODE

When I run it I get the following output:

//BEGIN OUTPUT
Mouse( ) this = 0xbffff857
Leaving foo( )
~Mouse( ) this = 0x804b498
Returned from foo
~Mouse( ) this = 0xbffff857
//END OUTPUT

As you can see there are two dtor calls but only one ctor call.

The second constructor call is to an implicitly defined copy
constructor. Try defining one yourself one and see what happens.

Mouse( const Mouse&) { cout << "Mouse( const Mouse&) this = "
this << "n"; }


Or even more interesting:

Mouse( const Mouse& other ) {
cout << "this = " << this << " other = " << &other << endl;
}

Johnathon and Daniel,

I feel stupid. Thank you both for pointing out what should have been
obvious.

-exits



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.