简体   繁体   中英

Will this code free memory allocated for MULTIMAP?

I have a multimap and a pointer to the multimap . Say i have multimap typedef multimap<class1, class2*> LO_Index; . I also have LO_Index * _index; Which points to multimap. To Free all the memory spaces allocated for this maop I am performing the following operation . I have a Reset function which does the following to free all the memory space alocated for this multimap :

    for ( LO_Index ::iterator i = _index->begin(); i != _index->end(); i++ )
            delete (*i).second;

    // Delete all entries in the index
    _index->erase( _index->begin(), _index->end() );

What i have read in case of set is that setname.clear() actually doesnot free all the allocated space . But in http://www.cplusplus.com/reference/stl/multimap/erase/ I found about erase that

This effectively reduces the container size by the number of elements removed, calling each element's destructor.

So i guess it will actually free the allocated spaces. But I want to confirm whether the code written in my reset function is actually freeing the memory or not.

For a multimap, delete (*i).second can't compile because you can only delete objects via a pointer and an int is not a pointer.

About the erase function: as far as you are concerned, the entries are removed from the container. The allocator might decide not to return the memory, but cache it for reuse, however, this matters only in limited cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM