开发者

How to deallocate object pointer in vector?

I have ;

class object {
                    // any private datas token in heap area 
             public : 
                   ~object () ; 

};

in function ;

 vector < object*> tmp ;

my question is ;


You seem to be a bit confused. There are two concepts at play here:

  • How do I, the owner of tmp, deallocate the objects referred to by the pointers within tmp?
  • What should the destructor for the object class (~object) do?

These are not really that related. When you are finished with the tmp vector, you must manually go through and call delete on each of its elements in order to deallocate the memory occupied by the object object that the element points to. This is assuming the elements were allocated with new, of course.

The purpose of the object destructor ~object is to deallocate everything that the object object owns, not to deallocate the object object itself. If the object object does not own any dynamically allocated data, it does not need to do anything.

In other words, when you write delete tmp[i], two things happen:

  1. *(tmp[i])::~object() is called
  2. The memory pointed to by tmp[i] is deallocated

Note that (2) happens even if (1) does absolutely nothing. The point of step (1) is to allow the object that is about to be deallocated to deallocate any of its member objects that need to be deallocated. The destructor's job is emphatically not to deallocate the object that it was invoked on.

By way of explicit example:

class object {
  private:
    int foo;
  public:
    object() : foo(42) {}
    ~object() { /* nothing to do here; foo is not dynamically allocated */ }
};

int main() {
  vector<object*> tmp;
  tmp.push_back(new object());

  // Do some stuff with tmp

  for (int i = 0; i < tmp.size(); ++i) {
    delete tmp[i]; // Calls ~object and deallocates *tmp[i]
  }
  tmp.clear();

  return 0;
}

Or, by contrast

class object {
  private:
    int* foo;
  public:
    object() : foo(new int()) { *foo = 42; }
    ~object() { 
      // Now since foo is dynamically allocated, the destructor
      // needs to deallocate it
      delete foo;
    }
};

int main() {
  vector<object*> tmp;
  tmp.push_back(new object());

  // Do some stuff with tmp

  for (int i = 0; i < tmp.size(); ++i) {
    delete tmp[i]; // Calls ~object (which deallocates tmp[i]->foo) 
                   // and deallocates *tmp[i]
  }
  tmp.clear();

  return 0;
}


I wrote this generic function template, which should help you:

template<typename FwdIterator>
void deleter(FwdIterator from, FwdIterator to)
{
   while ( from != to ) 
   {
       delete *from;
       from++;
   }
}

Example:

struct object { ~object() { cout << "object deleted" << endl; } };

int main() {

        vector<object*> objects;
        objects.push_back(new object());
        objects.push_back(new object());
        objects.push_back(new object());
        objects.push_back(new object());
        objects.push_back(new object());

        deleter(objects.begin(), objects.end()); //delete objects
        objects.clear(); //clear the vector
        return 0;
}

Output:

object deleted
object deleted
object deleted
object deleted
object deleted

Demo at ideone : http://www.ideone.com/t5PI0


You can simply call delete tmp[i];. But I would recommend to use std::vector< std::shared_ptr< object > > instead.


The destructor function should contain all necessary stuffs to free up the memory object is occupying.

Since this object pointer is stored inside a vector, you should remove reference to it after destroying it. That is, erase that object* from the vector.


To delete the object on the end of that pointer, call delete myVector[i]

To then remove that pointer from in the vector, call myVector.erase(myVector.begin()+i)


You put in the destructor whatever your class needs to free its members.

As for the vector, it contains pointers, not object instances. As such, calling erase() will only remove pointers from the vector, but will not free the objects that the pointers are pointing at. You have to free the objects separately, eg:

std::vector<object*> tmp;
tmp.push_back(new object);
...
std::vector<object*>::iterator iter = ...;
delete *iter;
tmp.erase(iter);


You can use vector::erase..

And your object destructor should delete all the members you have allocated in heap.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜