For instance I have static declaration of string
std::string s("test");
The memory will be allocated dynamically for the string. When the string s goes out of scope
the memory allocated for string will be deallocated ?
In case of std::map
std::map <std::string, std::string> testMap;
Similarly if this testMap also goes out of scope, the destructors of the std::string are called and string’s memory will be deallocated ?
Comments ?
Thanks in advance 🙂
In addition to answers already posted, I have to notice that
std::stringandstd::map(as well as other containers) use allocators for memory management. Particularly this means that afterdeallocate()they return memory back to allocator, but not to system directly, and it is allocator (or STL implementation) depended will it be returned to system or not. Because calling system memory management (likemalloc()) could be heavy enough, some allocators do not return memory, so next call toallocate()will be much faster if some preallocated block exists. Sometimes it may leads to spurious memory leaks detected by various tools.