I am using VC++ 6.0.

When i clear a vector using vector.clear() the capacity is not being reduced to zero. Hence the memory is not being deallocated.

I have tried to reduce the reserved size by use of vector.clear() followed by vector.reserve(0). This however has no effect. I have also tried swapping it with itself which also has no effect?

Is there a simple, proper way to do this or tell me what I might be doing incorrectly? Thanks!

My code for example:

std::vector<std::vector<CString> > myarray;
std::vector<CString> row;

myarray.clear(); //Doesnt deallocate the memory
myarray.reserve(0); //Doesnt deallocate memory
myarray.swap(myarray);//Doesnt deallocate memory

>myarray.clear(); //Doesnt deallocate the memory
Correct. Clearing the array only changes the size.

>myarray.reserve(0); //Doesnt deallocate memory
Correct. Reserving space only causes reallocation if the new size is greater than the current capacity.

>myarray.swap(myarray);//Doesnt deallocate memory
This isn't required to shrink-wrap the capacity. You might have better luck with a different object, but that isn't guaranteed either:

vector<vector<CString> >( myarray.begin(), myarray.end() ).swap ( myarray );

Hi Narue,

Thank you once again.

vector<vector<CString> >( myarray.begin(), myarray.end() ).swap ( myarray );

That works perfectly.
Appreciate the help!

This question has already been answered. Start a new discussion instead.