| View previous topic :: View next topic |
| Author |
Message |
exits funnel Guest
|
Posted: Fri Jan 30, 2004 3:08 am Post subject: Confusion about STL vector and memory management |
|
|
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
|
Posted: Fri Jan 30, 2004 3:29 am Post subject: Re: Confusion about STL vector and memory management |
|
|
"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
|
Posted: Fri Jan 30, 2004 3:38 am Post subject: Re: Confusion about STL vector and memory management |
|
|
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
|
Posted: Fri Jan 30, 2004 2:59 pm Post subject: Re: Confusion about STL vector and memory management |
|
|
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 |
|
 |
|