Rhyous

November 3, 2009

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

Filed under: Uncategorized — J. Abram barneck @ 3:55 am

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.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: