Click here to Skip to main content
13,258,269 members (41,109 online)


QuestionFree memory allocated by std::multimap Pin
khaliloenit4-Oct-12 3:41
memberkhaliloenit4-Oct-12 3:41 
AnswerRe: Free memory allocated by std::multimap Pin
pasztorpisti6-Oct-12 16:26
memberpasztorpisti6-Oct-12 16:26 
It might depend on the implementation of the multimap. I checked it long ago but if I remember right your erase() call sets back the allocation to a minimum, identical to the allocation performed by the default ctor of multimap (at least in the SGI stl implementation of Visual C++ I used at the time). Note that even newly created empty std containers have a small piece of memory preallocated and some containers (like std::vector) dont shrink the size of the allocated memory area (capacity) even if you erase items.

A trick that seems to reset the allocated memory of any std containers regardless of stl implementation and container type is the following:
typedef std::multimap<int,int> MyMap;
MyMap global_map;
void my_func()
    // We create a new map instance and we swap its contents with the other "big" container instance.
    // This swap operation replaces the pointers inside the two containers so after the swap() the
    // global_map contains the small newly allocated blocks, and empty_map contains the big mem blocks
    // previously owned by global_map. Note: when empty_map runs out of scope it releases the big block.
    // This trick works with other stl container types too.
    MyMap empty_map;

AnswerRe: Free memory allocated by std::multimap Pin
Stephen Hewitt11-Oct-12 22:53
memberStephen Hewitt11-Oct-12 22:53 
GeneralRe: Free memory allocated by std::multimap Pin
pasztorpisti11-Oct-12 23:53
memberpasztorpisti11-Oct-12 23:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 26 Oct 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid