Deallocation of memory

May 10, 2011 at 8:24am
Hey guys, i have 2 questions to ask. Firstly ,i would like to comfirm, does using clear()function on vector and multimap , helps to destroy the elements in the vector and deallocate memory space since the elements inside it was destroyed? If it isnt, what functions can i use to make sure memory allocated to it will be deallocated.
May 10, 2011 at 8:29am
vector::clear calls erase witch erases all elements. If you are holding pointers in your vector and used something like this:
1
2
3
4
5
class Foo;
...
vector<Foo*> v;
Foo* pfoo = new Foo;
v.push_back(pfoo)

you must free memory your self:
1
2
3
4
5
6
7
vector<Foo*>::iterator i;
for(i = v.begin(); i != v.end(); ++i)
{
    delete (*i);//delete data that was pointed
    *i = 0;
}
v.clear();
May 10, 2011 at 8:31am
so i first call each element and then delete each seperately . Then i proceed to clear the vector ?
May 10, 2011 at 8:45am
Yes but not necessary. And even better:
1
2
3
4
5
6
for(...)
{
...
}
v.clear();
v.shrink_to_fit();//this gets rid of all allocations 

But be carfull not to use pfoo pointer afterward as it points to freed memory and might crash your app!

Edit: If you can somehow gather thos "garbage" pointers, just assign them null:
1
2
3
4
5
6
7
8
9
10
11
12
13
vector<Foo*>::iterator i;
for(i = v.begin(); i != v.end(); ++i)
{
    delete (*i);//delete data that was pointed
    *i = 0;
}
v.clear();
v.shrink_to_fit();
...
pfoo0 = 0;
pfoo1 = 0;
pfoo2 = 0;
...
Last edited on May 10, 2011 at 8:48am
May 10, 2011 at 8:51am
How about just a normal string vector which does not contain pointers?

For example

1
2
3
4
5
6
7
8
9
10
11
12
 vector<string> HeaderVec2;
vector<string>::iterator i;

HeaderVec2.push_back("Name");
HeaderVec2.push_back("Randy");

for (i = HeaderVec2.begin(); i != HeaderVec2.end(); ++i)
{
        delete (*i);
}

HeaderVec2.clear();


When i tried doing this , i got an error " error: type ‘struct std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int>’ argument given to ‘delete’, expected pointer ". Is this due to the fact that, i just normally inserted values into my vector without using pointers?
May 10, 2011 at 8:56am
You don't need to must not call delete if you are not created elements on the heap with new.
In this case clear() & shrink_to_fit() is enough!

1
2
3
4
5
6
7
8
class Foo;
...
Foo f;
vector<Foo> v;
v.push_back(f);
...
v.clear();
v.shrink_to_fit();
Last edited on May 10, 2011 at 9:03am
May 10, 2011 at 9:09am
If you are unsure if you got memory leaks, i highly recommend this library:
http://www.softpedia.com/get/Programming/Components-Libraries/Visual-Leak-Detector.shtml

To moderators:
I can't find home page for this version of VLD (2.1) so i provided link from softpedia.
May 10, 2011 at 1:51pm
As far as I know there is no way to reduce the memory allocated to a std::vector. What you can do is either create the vector using new so you can delete it when you want to return the memory or put the vector in its own scope:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    // do some stuff

    { // introduce a new scope

        std::vector<int> v;
        // do stuff with vector

    } // end of scope, vector is destroyed returning memory

    // do some more stuff
}

Topic archived. No new replies allowed.