i'm trying to get a better understand of differences between the stack and the heap, especially since RAII is very important in C++. please see the following code:
ok, pretty simple code. however, Creation = 10, and Deletion = 42 after that loop is finished. i'm assuming that the push_back function copies data into the list, otherwise all the data would be gone after the loop exits...but why is the constructor never called more than the 10 times, and so many deletions?Code:static int Creation; static int Deletion; struct Test { int Value; Test() { ++Creation; } ~Test() { ++Deletion; } }; int main() { std::vector<Test> list; for (int i = 0; i < 10; ++i) { Test t; t.Value = i; list.push_back(t); } }
thanks.