How to delete a vector of pointers in C++?

Ok, so lets say you have a pointer to a vector of pointers and you delete the pointer to the vector. That will result in a memory leak.

Here is an example of a memory leak.

#include <vector>
using namespace std;
int main()
{
	// This is one pointer to a vector of pointers
	vector<int*> *intList1 = new vector<int*>();
	for (unsigned short i = 0; i < 10; i++)
	{
		intList1->push_back(new int(i));
	}
}

Ok, so now that I have shown you this code that can be a memory leak, lets look at some more code to see why.

Step through this code in a debugger and read the comments to help you understand what is happening.

#include <vector>

using namespace std;

/*
 * Pointer deleter for a vector of pointers
 */
template <class T>
void deleteVectorOfPointers( T * inVectorOfPointers )
{
    T::iterator i;
    for ( i = inVectorOfPointers->begin() ; i < inVectorOfPointers->end(); i++ )
    {
        delete * i;
    }
    delete inVectorOfPointers;
}

int main()
{
	// This is one pointer to a vector of pointers
	vector<int*> *intList1 = new vector<int*>();
	for (unsigned short i = 0; i < 10; i++)
	{
		intList1->push_back(new int(i));
	}

	// This is another pointer to a vector of the same pointers

	vector<int*> *intList2 = new vector<int*>();
	for (unsigned short i = 0; i < intList1->size(); i++)
	{
		intList2->push_back(intList1->at(i));
	}

	// This is a third pointer to a vector of the same pointers

	vector<int*> *intList3 = new vector<int*>();
	for (unsigned short i = 0; i < intList1->size(); i++)
	{
		intList3->push_back(intList1->at(i));
	}
	// Break here and look at the three vectors.
	// - Notice all 10 elements in each vector are the exact same
	// memory locations.
	// - Notice the memory locations hold values 0 thru 9

	// Delete the intList pointer
	delete intList1;

	// Break here and look at the three vectors.
	// One is deleted, but the other two are still there and all
	// the vectors' pointers are still there and did not delete.

	// Delete all the pointers in the vector.
	deleteVectorOfPointers(intList2);

	// Stop here and look at the three pointers.
	// Specifically look at the values of intList3.  They no longer
	// hold values 0 thr 9 because they were deleted since they
	// are the same pointer that intList2 had.

	// We don't need to delete the pointer in intList3 because
	// they were already deleted.
	delete intList3;
}

So in conclusion you can see that the problem with deleting the pointer to the vector is that deleting the pointer to the vector doesn’t delete the pointers added to the vector. You don’t want to leave 10 pointers hanging around in memory every time you use a piece of code.

You have to first delete the pointers added to the vector, and then delete the pointer to the vector.

3 Comments

  1. David says:

    Doesn't compile with more recent compilers...

    delete_vector_of_pointers.cpp: In function ‘void deleteVectorOfPointers(T*)’:
    delete_vector_of_pointers.cpp:11:5: error: need ‘typename’ before ‘T:: iterator’ because ‘T’ is a dependent scope
    delete_vector_of_pointers.cpp:11:17: error: expected ‘;’ before ‘i’
    delete_vector_of_pointers.cpp:12:11: error: ‘i’ was not declared in this scope
    delete_vector_of_pointers.cpp: In function ‘void deleteVectorOfPointers(T*) [with T = std::vector]’:
    delete_vector_of_pointers.cpp:56:36: instantiated from here
    delete_vector_of_pointers.cpp:11:5: error: dependent-name ‘T:: iterator’ is parsed as a non-type, but instantiation yields a type
    delete_vector_of_pointers.cpp:11:5: note: say ‘typename T:: iterator’ if a type is meant

    changing line 10 to "vector::iterator i;"

    • Rhyous says:

      David,

      Can you tell me what compiler you used. I dropped this into VS 2012 on Windows 8 and it works. I don't have a FreeBSD box to test it with GCC or clang at the moment.

      • David says:

        I used g++ on linux and cygwin

        I just reproduced the same error messages on cygwin
        gcc version 4.5.3 (GCC)

        The cygwin compilers can be download from:
        http://www.cygwin.com/install.html

        ( I bet you could use the MSYS distribution with gcc also, but I haven't tried that
        http://www.mingw.org/wiki/MSYS ( see http://www.mingw.org/wiki/Getting_Started )
        )

Leave a Reply to David

How to post code in comments?