开发者

C++ basics, vectors, destructors

I'm a little confused about the best practice for how to do this. Say I have a class that for example allocs some memory. I want it to self destruct like an auto but also put it in a vector for some reason unknown.

 #include <iostream>
 #include <vector>
 class Test {
 public:
  Test();
  Test(int a);
  virtual ~Test();
  int counter;
  Test * otherTest;
 };

 volatile int count = 0;


 Test::Test(int a) {
  count++;
  counter = count;
  std::cout << counter <<  "Got constructed!\n";
  otherTest = new Test();
  otherTest->counter = 999;
 }

 Test::Test() {
  count++;
  counter = count;
  std::cout << counter <<  "Alloced got constructed!\n";
  otherTest = NULL;
 }


 Test::~Test() {
  if(otherTest != 0){
  std::cout << otherTest->counter << " 1Got destructed" << counter << "\n";
  otherTest->counter = 888;
  std::cout << otherTest->counter << " 2Got destructed" << counter << "\n";
  }
 }

 int vectorTest(){
  Test a(5);
  std::vector<Test> vecTest;
  vecTest.push_back(a);
  return 1;
 }

 int main(){
  std::cout << "HELLO WORLD\n";
  vectorTest();
  std::cout << "Prog finished\n";
 }

In this case my destructor gets called twice all from counter 1, the alloc' object has already been set to 888 (or in a real case freed leading to bad access to a deleted object). What's the correct case for putting a local variable into a vector, is this some kind of design that would never happen sensibly. The following behaves differently and the destructor is called just once (which makes sense given its an alloc).

 int vectorTest(){
  //Test a(5);
  std::vector<Test> vecTest;
  vecTest.push_back(*(new Test(5)));
  return 1;
 }

How can I make the local variable behave the same leading to just one call to t开发者_StackOverflowhe destructor? Would a local simply never be put in a vector? But aren't vectors preferred over arrays, what if there are a load of local objects I want to initialize separately and place into the vector and pass this to another function without using free/heap memory? I think I'm missing something crucial here. Is this a case for some kind of smart pointer that transfers ownership?


A vector maintains its own storage and copies values into it. Since you did not implement a copy constructor, the default one is used, which just copies the value of the pointer. This pointer is thus deleted twice, once by the local variable destructor and once by the vector. Don't forget the rule of three. You either need to implement the copy and assignment operators, or just use a class that already does this, such as shared_ptr.

Note that this line causes a memory leak, since the object you allocated with new is never deleted:

vecTest.push_back(*(new Test(5)));


In addition to what Dark Falcon wrote: To avoid reallocating when inserting into a vector, you typically implement a swap function to swap local element with a default-constructed one in the vector. The swap would just exchange ownership of the pointer and all will be well. The new c++0x also has move-semantics via rvalue-references to help with this problem.


More than likely, you'd be better off having your vector hold pointers to Test objects instead of Test objects themselves. This is especially true for objects (like this test object) that allocate memory on the heap. If you end up using any algorithm (e.g. std::sort) on the vector, the algorithm will be constantly allocating and deallocating memory (which will slow it down substantially).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜