C++ Too many destructors called so few objects

Discussion in 'C++' started by Ravi, Aug 29, 2010.

  1. Ravi

    Ravi Guest

    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.
    Ravi, Aug 29, 2010
    #1
    1. Advertising

  2. Ravi

    Kai-Uwe Bux Guest

    Ravi wrote:

    > 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>;


    Should you have the habit of allocating vectors dynamically, you might want
    to reconsider the rationale for that.

    > 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.


    Because so many constructors are called: define the copy constructor and
    make it also print messages.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Aug 29, 2010
    #2
    1. Advertising

  3. Ravi

    Ravi Guest

    Thanks Pete and Kai-Uwe I got the answer.
    Ravi, Aug 29, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. peelman

    How many threads is too many?

    peelman, Jan 13, 2005, in forum: Java
    Replies:
    12
    Views:
    816
    Esmond Pitt
    Jan 15, 2005
  2. Pete Coolidge

    OnItemCreated and OnItemDataBound called too many times?

    Pete Coolidge, Jul 4, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    228
    Pete Coolidge
    Jul 4, 2003
  3. Dag Sunde
    Replies:
    4
    Views:
    344
  4. bgold12
    Replies:
    1
    Views:
    126
    Thomas 'PointedEars' Lahn
    Aug 10, 2008
  5. Alf P. Steinbach
    Replies:
    1
    Views:
    169
    Alf P. Steinbach
    Oct 15, 2013
Loading...

Share This Page