Is there any function that allows me to completely remove and deallocate all contents of a std::map? The functions clear()
only removes all elements, it doesn't free any memory.
tomtetlaw -1
Posting Pro
Recommended Answers
Jump to PostRather than put raw pointers, a common technique is to use shared pointers, which use reference counting. Otherwise need to need to iterate and delete your own objects
Jump to PostYou mean besides reducing the scope of the std::map object? You can swap it to an empty temporary container:
int main() { std::map<int,int> my_large_map; //... allocate a large number of elements in map. std::map<int,int>().swap(my_large_map); //swap with an empty temporary //at this point my_large_map is empty and the …
Jump to PostHello mike_2000_17. Would you mind explaining why that's a dirty hack? That's not dirty at all. It's a proper example of the power of RAII, exception-safe and an excellent tool in general. Global variables (and "dynamic variables," which this construct helps implement) have their place in software and it's better …
All 12 Replies
tomtetlaw -1
Posting Pro
template<> 37
Junior Poster
mike_2000_17 2,669
21st Century Viking Team Colleague Featured Poster
tomtetlaw -1
Posting Pro
mike_2000_17 2,669
21st Century Viking Team Colleague Featured Poster
Rashakil Fol 978
Super Senior Demiposter Team Colleague
mike_2000_17
commented:
-1 for suggesting such a dirty hack... +0
Rashakil Fol 978
Super Senior Demiposter Team Colleague
mike_2000_17 2,669
21st Century Viking Team Colleague Featured Poster
Rashakil Fol 978
Super Senior Demiposter Team Colleague
vijayan121 1,152
Posting Virtuoso
mike_2000_17 2,669
21st Century Viking Team Colleague Featured Poster
Rashakil Fol 978
Super Senior Demiposter Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.