views:

96

answers:

4

Hi, I have a vector that I fill with pointers to objects. I am trying to learn good memory management. I 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 (the vector goes out of scope)?

Thanks!

+5  A: 
  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.

David Titarenco
3: C++ will of course free the memory used by the pointers, since they are allocated on the stack. But the objects pointed to by those pointers are most likely allocated on the heap and thus will need to be deleted. And of course the pointers in the vector could point to stack allocated objects, which may not be deleted. Generally you should never store non-const pointers to stack allocated objects in a vector.
smerlin
Thanks! That was very clear!
Hashuer Hashing
+2  A: 

You can also use std::unique_ptr if you have access to C++0x. It replaces the deprecated std::auto_ptr that couldn't be used in containers.

David
+2  A: 

Everything you allocate with new you have to delete later on. Objects that you don't explicitly allocate with new shouldn't you delete.

If you don't want to manage the objects manually but want the vector to "own" them, it might be better to store the objects by value instead of storing pointers to them. So instead of std::vector<SomeClass*> you could use std::vector<SomeClass>.

sth
A: 

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());
FredOverflow