Weird behavior when creating vector objects

I have written a simple pgm to demonstrate a little problem I am having. Have a look at the pgm and the generated output:

/**************************************************/
/**************************************************/
/**************************************************/
#include <iostream.h>

#include <vector>
using namespace std;

class foo{
public:
inline foo(int i){cout << "Value of i is " << i << endl;}

inline ~foo(){cout << "Destroying..." << endl;}
};

void main(){
vector<class foo> Vfoo;

Vfoo.push_back(1);
Vfoo.push_back(2);
Vfoo.push_back(3);
Vfoo.push_back(4);
Vfoo.push_back(5);
}
/**************************************************/
/**************************************************/
/**************************************************/

Value of i is 1
Destroying...
Value of i is 2
Destroying...
Destroying...
Value of i is 3
Destroying...
Destroying...
Destroying...
Value of i is 4
Destroying...
Value of i is 5
Destroying...
Destroying...
Destroying...
Destroying...
Destroying...
Destroying...
Destroying...
Destroying...
Destroying...
Destroying...
Press any key to continue

Clearly too many destructors are being called and I have not been able to figure out why? Any help would be greatly appreciated.

Thanks in advance,

-Fab-
[1561 byte] By [f_carrie] at [2007-11-18 17:42:12]
# 1 Re: Weird behavior when creating vector objects
That is because internally, the vector<> uses copy ctor to copy the value when you push_back the value. You can write a copy ctor to show that it is invoked for every push_back(). When you push_back with a value, the compiler implicitly created a temporary foo instance. It is then copied into a new foo instance created by vector<> via copy ctor. Based on this, if you reserve enough memory for vector, you should be able to see 10 foo instances created and destroyed. However, if you didn't reserve and when vector<> runs out of memory, it needs to reallocate memory and thus using copy ctor to copy all existing foo instances into the newer memory space.

class foo{
public:
foo(int i) : num(i){cout << "Value of i is " << i << endl;}
foo(const foo& f):num(f.num) {cout << "Value of i is " << num << " in copy ctor" << endl;}

~foo(){cout << "Destroying..." << endl;}
int num;
};

int main()
{
vector<class foo> Vfoo;

// Should try with and without reserve().
Vfoo.reserve(5);

Vfoo.push_back(1);
Vfoo.push_back(2);
Vfoo.push_back(3);
Vfoo.push_back(4);
Vfoo.push_back(5);

return 0;
}
Kheun at 2007-11-9 0:30:49 >
# 2 Re: Weird behavior when creating vector objects
Add a copy constructor to your foo class and you'll see the match for each destructor ( push_back creates them).

inline foo(const foo& f){cout << "FOO copy created" << endl;}
PadexArt at 2007-11-9 0:31:49 >
# 3 Re: Weird behavior when creating vector objects
[Merged threads]
Andreas Masur at 2007-11-9 0:32:55 >
# 4 Re: Weird behavior when creating vector objects
Originally posted by f_carrie
#include <iostream.h>

#include <vector>
using namespace std;Don't use <iostream.h>; use <iostream>.
Sam Hobbs at 2007-11-9 0:34:01 >
# 5 Re: Weird behavior when creating vector objects
Great! I guess thats why they call it code"guru". Sam I agree with your last remark and will be more concious of it in future posts. Thanks for all your help guys!

-Fab-
f_carrie at 2007-11-9 0:35:00 >