Confusion about STL vector and memory management

E

exits funnel

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 foo\n";
}
void foo(Mouse* mptr)
{
vector<Mouse> v;
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
 
J

Jonathan Turkanis

exits funnel said:
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 foo\n";
}
void foo(Mouse* mptr)
{
vector<Mouse> v;
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
 
D

Daniel T.

Jonathan Turkanis said:
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;
}
 
E

exits funnel

Daniel said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top