🔒 Locked
Freeing a std::map
Advertisement
Original Post
If I had to free an entire std::map object which contains an array of std::map's inside, can I still use the clear() function or do I have to manually go through each element in the map array and clear them first in order to avoid memory leaks? Thanks
I have the following:
Is that ok?
std::map< int, file_section_t > mFileSection;std::map< int, std::map< int, std::string > > mCacheSection;Is that ok?
Quote:
I don't know what a file_section_t is so I couldn't say about the first one. But the second one you should be able to just clear() to free (most of) the memory associated with it.
Oops, sorry, its just a structure.
Thanks for helping me.
It only has int's.
Thanks
Thanks
To generalise: When you call std::map::clear(), the destructor is called for every key and value in the map. All STL objects will free their memory and call the destuctors for all elements inside them, including std::string and std::map.
If you have pointers (or references) in your container, they don't have destructors, so you'd have to manually delete them, either by iterating through the container and deleting what the iterator is pointing to, or if you reference the data elsewhere, free it from there.
If you use a custom class or strust in your container, then you should either not implement any constructors, destructors or copy operators and use the default (compiler-implemented) ones, or you should implement a default constructor, copy constructor, destructor, and assignment operator, so that the STL object can properly copy your type around internally.
If you have pointers (or references) in your container, they don't have destructors, so you'd have to manually delete them, either by iterating through the container and deleting what the iterator is pointing to, or if you reference the data elsewhere, free it from there.
If you use a custom class or strust in your container, then you should either not implement any constructors, destructors or copy operators and use the default (compiler-implemented) ones, or you should implement a default constructor, copy constructor, destructor, and assignment operator, so that the STL object can properly copy your type around internally.
Thanks for the info.
The thing is that I havn't actually used STL stuff untill today (except for vector [wink]) and I just wanted to make sure that I don't cause any unnecessary memory leakes.
I wasn't sure if it would call the destructors/constructors on creation/deletion of std::maps.
The thing is that I havn't actually used STL stuff untill today (except for vector
I wasn't sure if it would call the destructors/constructors on creation/deletion of std::maps.
The Standard Library containers play fair: they clean up their toys, and leave you to clean up your toys (because it can't know whether you're really done with them anyway).
Incidentally, you should design your own classes this way, too (when they have "toys" to speak of, i.e. allocated memory that "belongs" to the class instance).
Incidentally, you should design your own classes this way, too (when they have "toys" to speak of, i.e. allocated memory that "belongs" to the class instance).
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement