473,668 Members | 2,348 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 22 '05 #1
3 1276
"exits funnel" <ex*********@NO SPAMyahoo.com> wrote in message
news:40******** ******@NOSPAMya hoo.com...
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(*mp tr);
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
Jul 22 '05 #2
In article <bv************ @ID-216073.news.uni-berlin.de>,
"Jonathan Turkanis" <te******@kanga roologic.com> wrote:
"exits funnel" <ex*********@NO SPAMyahoo.com> wrote in message
news:40******** ******@NOSPAMya hoo.com...
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(*mp tr);
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;
}
Jul 22 '05 #3


Daniel T. wrote:
In article <bv************ @ID-216073.news.uni-berlin.de>,
"Jonathan Turkanis" <te******@kanga roologic.com> wrote:

"exits funnel" <ex*********@NO SPAMyahoo.com> wrote in message
news:40****** ********@NOSPAM yahoo.com...
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(*mp tr);
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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3064
by: Tran Tuan Anh | last post by:
Dear all, I am new in C++, and now get confused about a lot of things. I wrote this simple code to test the vector. class Temp { public: int x; }; int main() { vector<Temp> v;
9
2968
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version seem to work. I would appreciate someone to comment on this. Version 1 (crashes on deallocating) #include <iostream>
12
2331
by: BCC | last post by:
If I create a vector of vectors of double: std::vector< std::vector<double> > table1; Are my vectors of doubles uninitialized? Do I have to loop through table1 and initialize each vector of doubles using new? And in cleaning up, manually delete each of these vectors of doubles? Thanks,
7
1150
by: Mona | last post by:
class myPoly { private: int nv; double *x, *y; int *p; public: myPoly() {
12
3937
by: Alfonso Morra | last post by:
I have the ff code for testing the concept of storing objects: #include <vector> #include <iostream> using namespace std ; class MyClass { public: MyClass(){
13
4641
by: Richard | last post by:
vector<char*> m_Text; m_Text.resize(1); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1); if (foobar) { strcpy(foobar, foo); strcat(foobar, bar); }
2
4751
by: Jef Driesen | last post by:
I'm working on a project where i need to exchange multidimensional data between C/C++ (row-major) and matlab (column-major). I understand the difference between those two mappings to linear memory. Suppose I need an S1 x S2 x ... x Sn dimensional array A. I can have the same layout in memory by reversing the dimensions: A... == A(Sn,...,S1) where I used the C/C++ notation for row-major and the matlab notation
13
2882
by: smp | last post by:
Does anyone know why making a vector of pointers is so much less efficient than a vector of objects? For a simple example: int num = 20; vector<int*v_int_ptr; v_int_ptr.reserve(num); for(int i = 0; i < num; i++) { int* my_int_ptr = new int;
5
505
by: cham | last post by:
Hi, I am working on c++ in a linux system ( Fedora core 4 ), kernel version - 2.6.11-1.1369_FC4 gcc version - 4.0.0 20050519 ( Red Hat 4.0.0-8 ) In my code i am creating a vector to store pointers of type structure "SAMPLE_TABLE_STRUCT" ( size of this structure is 36 bytes ). I create an instance of structure "SAMPLE_TABLE_STRUCT" using operator "new" and push back into the vector,this is done inside a for loop for
0
8459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.