how does copy constructor works with vector::push_back
-
I'm trying to figure out how exactly vector::push_back() creates objects. Looks like vector::push_back() will create a lot of copies by copy constructor and destruct a lot. In other words, if I need to put a counter in the copy constructor to monitor how many copy of the objects are created during the run, I will get a number which I don't exactly understand why. Can anybody explain why push_back() keep making copies and deleting copies? By running the following sample program, I supose to get 2,4,6,8,10 as output, but I get 21,22,23,24,25. Why is that? Thanks! class CVecTest { public: CVecTest() {m_intCount = ++m_classNumber;} CVecTest::CVecTest(const CVecTest &vec) {m_intCount = ++m_classNumber;} ~CVecTest(){}; static int m_classNumber; int m_intCount; }; int CVecTest::m_classNumber = 0; int main(int argc, char* argv[]) { vector vec; for (int i=0; i<5; i++) { CVecTest p; vec.push_back(p); } for (int i=0; i<5; i++) cout << vec[i].m_intCount << endl; return 0; } :confused:
All STL containers have value semantics and as such copy or assign elements. Steve
-
I'm trying to figure out how exactly vector::push_back() creates objects. Looks like vector::push_back() will create a lot of copies by copy constructor and destruct a lot. In other words, if I need to put a counter in the copy constructor to monitor how many copy of the objects are created during the run, I will get a number which I don't exactly understand why. Can anybody explain why push_back() keep making copies and deleting copies? By running the following sample program, I supose to get 2,4,6,8,10 as output, but I get 21,22,23,24,25. Why is that? Thanks! class CVecTest { public: CVecTest() {m_intCount = ++m_classNumber;} CVecTest::CVecTest(const CVecTest &vec) {m_intCount = ++m_classNumber;} ~CVecTest(){}; static int m_classNumber; int m_intCount; }; int CVecTest::m_classNumber = 0; int main(int argc, char* argv[]) { vector vec; for (int i=0; i<5; i++) { CVecTest p; vec.push_back(p); } for (int i=0; i<5; i++) cout << vec[i].m_intCount << endl; return 0; } :confused:
std::vector
and its friends in the C++ standard library will copy an added element and store the copy, not the original.std::vector
doesn't destroy a contained object - it's the object destructor that handles destruction when thestd::vector
goes out of scope. There's no container in the C++ standard library, which can share pointers that will be deleted when the last reference to them die. There are such alternatives in Boost[^] and Loki[^]. -- The Blog: Bits and Pieces -
std::vector
and its friends in the C++ standard library will copy an added element and store the copy, not the original.std::vector
doesn't destroy a contained object - it's the object destructor that handles destruction when thestd::vector
goes out of scope. There's no container in the C++ standard library, which can share pointers that will be deleted when the last reference to them die. There are such alternatives in Boost[^] and Loki[^]. -- The Blog: Bits and PiecesBut what I expect is, when an object is inserted to a container, the container will call its copy constructor to make a copy and put into the container. Based on the result of my testing, looks like there are more than one copy of the object is created by copy constructor during the "push_back" process, and some of them got deleted, finnally only more copy remained and stored into the container. Looks like the insertion process is more complicated than just a value semantic.
-
But what I expect is, when an object is inserted to a container, the container will call its copy constructor to make a copy and put into the container. Based on the result of my testing, looks like there are more than one copy of the object is created by copy constructor during the "push_back" process, and some of them got deleted, finnally only more copy remained and stored into the container. Looks like the insertion process is more complicated than just a value semantic.
Does this help? I suspect the additional copies may be the result of calling a function as in this example. ---------------- #include #include using namespace std; class CLogMe { public: CLogMe() { cout << "CLogMe()" << endl; } CLogMe(const CLogMe &) { cout << "CLogMe(const CLogMe &)" << endl; } CLogMe& operator=(const CLogMe &) { cout << "operator=(const CLogMe &)" << endl; return *this; } }; typedef vector LogMes; LogMes g_LogMes; void Function(CLogMe me) { g_LogMes.push_back(me); // CLogMe copy constructor called here. } int main(int argc, char* argv[]) { CLogMe me; // CLogMe constructor called here. Function(me); // CLogMe copy constructor called here. return 0; } Steve
-
Does this help? I suspect the additional copies may be the result of calling a function as in this example. ---------------- #include #include using namespace std; class CLogMe { public: CLogMe() { cout << "CLogMe()" << endl; } CLogMe(const CLogMe &) { cout << "CLogMe(const CLogMe &)" << endl; } CLogMe& operator=(const CLogMe &) { cout << "operator=(const CLogMe &)" << endl; return *this; } }; typedef vector LogMes; LogMes g_LogMes; void Function(CLogMe me) { g_LogMes.push_back(me); // CLogMe copy constructor called here. } int main(int argc, char* argv[]) { CLogMe me; // CLogMe constructor called here. Function(me); // CLogMe copy constructor called here. return 0; } Steve
-
But what I expect is, when an object is inserted to a container, the container will call its copy constructor to make a copy and put into the container. Based on the result of my testing, looks like there are more than one copy of the object is created by copy constructor during the "push_back" process, and some of them got deleted, finnally only more copy remained and stored into the container. Looks like the insertion process is more complicated than just a value semantic.
Ajax95 wrote:
the container will call its copy constructor to make a copy and put into the container
The compiler, if any, makes the copy ctor call.
Ajax95 wrote:
Based on the result of my testing, looks like there are more than one copy of the object is created by copy constructor during the "push_back" process
Not strange at all. The documented fact for a container with value semantics is that a copy of an element is added. It's not stipulated that one and only one copy will be made during the process and that that single copy is the one to be added. In fact, if you had taken the time to step into the
push_back
code while debugging, you would have noticed that lots of temporary copies are created during the process, which ensures that the original data is not tampered with.Ajax95 wrote:
some of them got deleted
Of course, that's the temporary ones that goes out of scope at each call return.
Ajax95 wrote:
Looks like the insertion process is more complicated than just a value semantic.
No. -- The Blog: Bits and Pieces
-
Does this help? I suspect the additional copies may be the result of calling a function as in this example. ---------------- #include #include using namespace std; class CLogMe { public: CLogMe() { cout << "CLogMe()" << endl; } CLogMe(const CLogMe &) { cout << "CLogMe(const CLogMe &)" << endl; } CLogMe& operator=(const CLogMe &) { cout << "operator=(const CLogMe &)" << endl; return *this; } }; typedef vector LogMes; LogMes g_LogMes; void Function(CLogMe me) { g_LogMes.push_back(me); // CLogMe copy constructor called here. } int main(int argc, char* argv[]) { CLogMe me; // CLogMe constructor called here. Function(me); // CLogMe copy constructor called here. return 0; } Steve
I'd say the additional copies comes from temporaries during the process of adding an element. -- The Blog: Bits and Pieces
-
All STL containers have value semantics and as such copy or assign elements. Steve
Stephen Hewitt wrote:
All STL containers have value semantics
For the next version of C++ standard it is planned to add move semantics[^]. That would be :cool:
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
-
Stephen Hewitt wrote:
All STL containers have value semantics
For the next version of C++ standard it is planned to add move semantics[^]. That would be :cool:
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
In the mean time you can get these semantics by butting smart pointers in the container - Boost has many such pointers. Steve
-
I'm trying to figure out how exactly vector::push_back() creates objects. Looks like vector::push_back() will create a lot of copies by copy constructor and destruct a lot. In other words, if I need to put a counter in the copy constructor to monitor how many copy of the objects are created during the run, I will get a number which I don't exactly understand why. Can anybody explain why push_back() keep making copies and deleting copies? By running the following sample program, I supose to get 2,4,6,8,10 as output, but I get 21,22,23,24,25. Why is that? Thanks! class CVecTest { public: CVecTest() {m_intCount = ++m_classNumber;} CVecTest::CVecTest(const CVecTest &vec) {m_intCount = ++m_classNumber;} ~CVecTest(){}; static int m_classNumber; int m_intCount; }; int CVecTest::m_classNumber = 0; int main(int argc, char* argv[]) { vector vec; for (int i=0; i<5; i++) { CVecTest p; vec.push_back(p); } for (int i=0; i<5; i++) cout << vec[i].m_intCount << endl; return 0; } :confused:
To avoid these extra copy constructor, call reserve function. like vec.reserve(5); Everytime you calls push_back, vector has to expand the memory block and copies the elements from the original to new memory block. Here comes the copy constructor in picture. As the size of vector increases, the number of calls of copy constuctor keeps on increasing(directly proportional to number of elements in vector).