How to free an object pointer in a vector?

I have:

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

in function;

  vector < object*> tmp ; 

my question is:

  • How to erase / free tmp [i] ? In other words, what should I write in the object :: destructor?
+4
source share
7 answers

You seem a little confused. There are two concepts here:

  • How do I, the owner of tmp , free objects referenced within tmp ?
  • What should the destructor do for the class object ( ~object )?

This is actually not the case. When you are done with the tmp vector, you must manually go through and call delete for each of your elements to free the memory occupied by the object pointed to by the element. This suggests that the elements were highlighted using new , of course.

The purpose of the object ~object descriptor is to free everything that belongs to the object , rather than free the object . If the object object does not have any dynamically allocated data, it does not need to do anything.

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

  • *(tmp[i])::~object() is called
  • The memory pointed to by tmp[i] is freed.

Note that (2) happens even if (1) does absolutely nothing. The step (1) should allow the object to be freed in order to free any of its member objects that need to be freed. The task of the destructor does not decisively release the object on which it was called.

As an 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 vice versa,

 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; } 
+15
source

I wrote this generic function template that 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 

Ideon demo: http://www.ideone.com/t5PI0

+3
source

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

+1
source

The destructor function must contain all the necessary materials to free the memory object.

Since this object pointer is stored inside the vector, you must delete the link to it after it is destroyed. That is, remove this object * from the vector.

0
source

To delete an object at the end of this pointer, call delete myVector [i]

To remove this pointer from a vector, call myVector.erase (myVector.begin () + i)

0
source

You put a destructor in any class to free its members.

As for the vector, it contains pointers, not instances of objects. Thus, calling erase() removes pointers only from the vector, but does not free the objects pointed to by pointers. You must free objects separately, for example:

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

You can use vector :: erase ..

And your object destructor should delete all the members that you allocated on the heap.

-1
source

Source: https://habr.com/ru/post/1343591/


All Articles