简体   繁体   中英

How can i make sure that memory allocated in case of MULTIMAP is freed

I have a multimap from which I want to free the memory allocated to multimap.

I have posed a query related to this in one of the other question in Stackoverflow Will this code free memory allocated for MULTIMAP? .

My question is that whether using an erase in multimap makes sure if all memory space allocated for multimap is freed ?

The standard does not specify whether erasing an element from a container will deallocate all memory for that element; the only guarantee is that the destructor of each erased object will be called exactly once. I can't imagine why you might need a guarantee that the memory is deallocated, but if you do then you'll have to check the particular implementation you're using.

If you use a clear() (or erase() with begin() and end()), all memory for items That the multimap have allocated will be released.

This works perfectly well for say, stocking ints.

If you allocate memory and then stock pointers in a multimap, you'll need to free that memory up yourself.

It works "as usual", you allocate -> you free it up, for ints, chars and multimaps, it is up to them to free memory they have allocated.

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