Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Too many destructors called so few objects [duplicate]

Here is the code (also at http://pastebin.com/yw5z2hnG ):

#include <iostream>
#include <vector>
using namespace std;

class X
{
    public:
    int i;
    X();
    ~X();
};

X::X()
{
    i = 1;
    cout << "---constructor" << '\n';
}

X::~X()
{
    cout << "***desctructor" << '\n';
}

int main()
{
    vector<X> *vx = new vector<X>;
    cout << "------------------------------------" << endl;
    vx->push_back(X());
    vx->push_back(X());
    vx->push_back(X());
    vx->push_back(X());
    vx->push_back(X());
    cout << "------------------------------------" << endl;
    delete vx;
}

I get the output as:

------------------------------------
---constructor
***desctructor
---constructor
***desctructor
***desctructor
---constructor
***desctructor
***desctructor
***desctructor
---constructor
***desctructor
---constructor
***desctructor
***desctructor
***desctructor
***desctructor
***desctructor
------------------------------------
***desctructor
***desctructor
***desctructor
***desctructor
***desctructor

I do not understand why so many destructors are called.

like image 889
Xolve Avatar asked Aug 29 '10 10:08

Xolve


2 Answers

If you define your own copy constructor you will see the other objects being constructed:

class X
{
    public:
    int i;
    X(const X&);
    X();
    ~X();
};

X::X(const X& x) : i( x.i )
{
    cout << "---copy constructor\n";
}

// ... rest as before

The compiler will provide a copy constructor that performs no logging if you don't declare your own one.

like image 195
CB Bailey Avatar answered Sep 22 '22 10:09

CB Bailey


One more detail - if you reserve space for your vector in advance:

int main()
{
    vector<X> *vx = new vector<X>;
    vx->reserve(5);
    ....

Then you'll get the following output:

---constructor
+++ copy constructor
***desctructor
---constructor
+++ copy constructor
***desctructor
...

So as you can see vector also copies all its elements when it is needed to reallocate its storage - that's why you get 5 copy constructors and destructors for the last push_back()

like image 33
Vladimir Avatar answered Sep 21 '22 10:09

Vladimir