I am having a much harder time than I should trying to deallocate the memory used by an STL map. Some example code:
void mainfunc()
{
testmap();
testvect();
}
void testmap()
{
map<int, float> testmap;
for (int i = 0; i < 1000000; i++) {
testmap[i] = (float)i;
}
testmap.clear();
}
void testvect()
{
vector<int> testvect;
for (int i = 0; i < 10000000; i++) {
testvect.insert(testvect.end(), i);
}
testvect.clear();
}
My code creates a STL map and then when it is done with it, I want the memory to be deallocated. I use a debugger and put a breakpoint in the mainfunc() and look at the memory used by the process. In this example, the testvect function works as I would expect, in that the for loop inserts a whole bunch of elements in the vector, so while it is in testvect() the memory usage increases and when testvect returns back to mainfunc(), the destructor is automatically called on the vector and the memory is deallocated.
However, the testmap() function doesn't work the same way. Memory usage goes up while it is inserting elements into the map but when the testmap() function returns and goes back to mainfunc(), the process is still using the same amount of memory. The memory is never getting deallocated. I feel like I must be doing something stupid as this seems so simple. Can anyone point out what I'm doing wrong? I'm using G++ 3.4.6. Thanks.