Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Vector of Objects

19 views
Skip to first unread message

Christian Steins

unread,
Nov 20, 2016, 11:18:35 AM11/20/16
to
Hi,
following program gives this output:

name = c1 x = 1 y = 2
name = c2 x = 3 y = 4
destructor c1
destructor c2
destructor c1
destructor c1
destructor c2

I don't understand why the destructor is called 5 times and the sequence.

Compiler is TDM-GCC 4.9.2.

Bug or expected behaviour?

Should one avoid to store objects in a vector?
Better to store object pointers?


--- snip -----------------------------------

// c++ test program

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class myclass {

private:
string name;
int x;
int y;

public:
void print() {
cout << "name = " << name;
cout << " x = " << x << " y = " << y << endl;
}

myclass() { x = 0; y = 0; name = "unnamed"; }
myclass(string nam, int xx, int yy) { x = xx; y = yy; name = nam; }

~myclass() {
cout << "destructor " << name << endl;
}
};

int main() {

vector<myclass> myvec;

myclass c1("c1",1,2);
myclass c2("c2",3,4);

c1.print();
c2.print();

myvec.push_back(c1);
myvec.push_back(c2);

return 0;
}

--- snip -----------------------------------

Rgds,
Christian

Öö Tiib

unread,
Nov 20, 2016, 11:47:43 AM11/20/16
to
On Sunday, 20 November 2016 18:18:35 UTC+2, Christian Steins wrote:
> Hi,
> following program gives this output:
>
> name = c1 x = 1 y = 2
> name = c2 x = 3 y = 4
> destructor c1
> destructor c2
> destructor c1
> destructor c1
> destructor c2
>
> I don't understand why the destructor is called 5 times and the sequence.

Why don't you explain what you did expect and in what order and why?
Then we can see where your logic fails. For me 5 destructor calls
seems like expected and order seems OK.

After 'myvec.push_back(c1);' the vector has one element and capacity for
one element so 'myvec.push_back(c2);' causes it to allocate more storage,
copy 'c1' there and to destroy the old storage and so call destructor for
that 'c1' in it.
After that the 'main' ends that causes destruction of original 'c2'
original 'c1' and then 'myvec' and so copies of 'c1' and 'c2' in that
'myvec'. So 5 calls.

Christian Steins

unread,
Nov 20, 2016, 12:30:29 PM11/20/16
to
Am 20.11.2016 um 17:47 schrieb Öö Tiib:

>> I don't understand why the destructor is called 5 times and the sequence.
>
>
> After 'myvec.push_back(c1);' the vector has one element and capacity for
> one element so 'myvec.push_back(c2);' causes it to allocate more storage,
> copy 'c1' there and to destroy the old storage and so call destructor for
> that 'c1' in it.
> After that the 'main' ends that causes destruction of original 'c2'
> original 'c1' and then 'myvec' and so copies of 'c1' and 'c2' in that
> 'myvec'. So 5 calls.

I see. Thanks for explanation.

Christian



Jorgen Grahn

unread,
Nov 20, 2016, 12:46:13 PM11/20/16
to
And: this behavior of the standard containers (that they actually
/contain/ objects) has turned out to be pretty successful. It rarely
becomes a performance issue.

I remember worrying about that back in 2001 or so; perhaps that's what
the OP is worrying about today, although he fails to communicate it.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
0 new messages