-1

I have following code in C++ using vector

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

class DataValue {
public:

    DataValue() { std::cout << "DataValue constructor called" << std::endl; }
    DataValue(DataValue const& other) { cout << "DataValue copy constructor called" << std::endl; }
    ~DataValue() { std::cout << "DataValue destructor is called" << std::endl; }
private:

};

class ItemDataHistory {
public:
    ItemDataHistory() { std::cout << "ItemDataHistory constructor called" << std::endl; }
    ItemDataHistory(ItemDataHistory & other) { std::cout << "ItemDataHistory copy constructor called" << std::endl; }
    ~ItemDataHistory() { std::cout << "ItemDataHistory destructor called" << std::endl; }
    std::vector<DataValue>& GetVecDataValues() { return m_vecDataValues; }  

private:
    std::vector<DataValue> m_vecDataValues;
};

class DataReply {
public:

    std::vector<ItemDataHistory>& GetItemDataHistories() { return m_vecItemData; }  

private:
    // The list of DataValue
    std::vector<ItemDataHistory> m_vecItemData;
};



void main()
{
    DataValue dv1, dv2, dv3;
    ItemDataHistory itmDH;
    itmDH.GetVecDataValues().push_back(dv1);
    itmDH.GetVecDataValues().push_back(dv2);
    itmDH.GetVecDataValues().push_back(dv3);

    return;
}

/*
DataValue constructor called
DataValue constructor called
DataValue constructor called
ItemDataHistory constructor called
DataValue copy constructor called
DataValue copy constructor called
DataValue copy constructor called

**DataValue destructor is called** // why this destructor is called? where constructor for this.

DataValue copy constructor called
DataValue copy constructor called
DataValue copy constructor called

**DataValue destructor is called  // why this destructor is called? Where is constructor for this.
DataValue destructor is called**  // why this destructor is called? Where is contructor for this.

ItemDataHistory destructor called
DataValue destructor is called
DataValue destructor is called
DataValue destructor is called
DataValue destructor is called
DataValue destructor is called
DataValue destructor is called
Press any key to continue . . .
*/

My question on above code is I am not getting why additional destructors are called. I am trying to understand performance by checking when constructors and destructors are called. I am using VS 2008 in windows XP.

Thanks for your time and help

venkysmarty
  • 11,099
  • 25
  • 101
  • 184

2 Answers2

3

push_back might want to increase the size of the vector, which means the old elements are copied to a new, bigger place and then destroyed. This is probably what's going on here.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • if copying is done then we should have copy constructor called but that output is not done. Is it using default assignment operator while copying to new vector of large size/ – venkysmarty Mar 06 '13 at 09:01
  • @venkysmarty no, that's called copy elision. See http://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization – Luchian Grigore Mar 06 '13 at 09:04
2

As an addition to the other answers: For a nice visualization, simply add

itmDH.GetVecDataValues().reserve(3);

before you push_back stuff, it will result in the output you'd expect to see in the first place: liveworkspace demo

It may also be instructive to use your code without reserve and inspect the capacity before and after each push_back (liveworkspace demo #2) :

DataValue constructor called
DataValue constructor called
DataValue constructor called
ItemDataHistory constructor called
0
DataValue copy constructor called
1 //reallocation of vector happens
DataValue copy constructor called //copy first element from old memory location
DataValue copy constructor called //copy the element to be inserted
DataValue destructor is called //destroy element at old memory location
2 //reallocation of vector happens
DataValue copy constructor called //copy first element from old memory location
DataValue copy constructor called //copy second element from old memory location
DataValue copy constructor called //copy the element to be inserted
DataValue destructor is called //destroy first element at old memory location
DataValue destructor is called //destroy second element at old memory location
4
...
us2012
  • 16,083
  • 3
  • 46
  • 62