Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do I need to do before deleting elements in a vector of pointers to dynamically allocated objects?

Tags:

I have a vector that I fill with pointers to objects. I am trying to learn good memory management, and have a few general questions:

  1. Is it true that when I am done with the vector, I must loop through it and call delete on each pointer?
  2. Why don't I have to call delete on the vector or any other variable I declare without the new statement, but delete must be called on pointers?
  3. Does C++ handle freeing the pointers' memory for me if the vector is declared in a function which returns (causing the vector to go out of scope)?
like image 882
Hashuer Hashing Avatar asked Oct 31 '10 01:10

Hashuer Hashing


2 Answers

  1. Yes
  2. Vectors are implemented using template memory allocators that take care of the memory management for you, so they are somewhat special. But as a general rule of thumb, you don't have to call delete on variables that aren't declared with the new keyword because of the difference between stack and heap allocation. If stuff is allocated on the heap, it must be deleted (freed) to prevent memory leaks.
  3. No. You explicitly have to call delete myVec[index] as you iterate over all elements.

Ex:

for(int i = 0; i < myVec.size(); ++i)
   delete myVec[i];

With that said, if you're planning on storing pointers in a vector, I strongly suggest using boost::ptr_vector which automatically takes care of the deletion.

like image 61
David Titarenco Avatar answered Sep 22 '22 02:09

David Titarenco


Is it true that when I am done with the vector I must loop through it and call delete on each pointer?

Well, you don't have to loop by hand, you can also use an algorithm:

#include <vector>
#include <algorithm>
#include <memory>

int main()
{
    std::vector<Base*> vec;
    vec.push_back(new Derived());
    vec.push_back(new Derived());
    vec.push_back(new Derived());

    // ...

    std::for_each(vec.begin(), vec.end(), std::default_delete<Base>());
}

If you don't have a C++0x compiler, you can use boost:

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/construct.hpp>

std::for_each(vec.begin(), vec.end(), boost::lambda::delete_ptr());

Or you can write your own functor:

struct delete_ptr
{
    template <class T>
    void operator()(T* p)
    {
        delete p;
    }
};

std::for_each(vec.begin(), vec.end(), delete_ptr());
like image 30
fredoverflow Avatar answered Sep 21 '22 02:09

fredoverflow