|
Hello,
I have the multimap :
TranslationTypeMap m_TranslationsMap;
where
typedef std::multimap< std::pair<CString, CString>, structTranslationData> TranslationTypeMap;
and
struct structTranslationData
{
std::wstring wstrTranslationId;
std::wstring wstrTextTranslated;
};
here an example of insertion in this map:
structTranslationData pstTranslationData;
pstTranslationData.wstrTranslationId = xxxx; pstTranslationData.wstrTextTranslated = xxxx;
strIdCaption = yyyy;
strIdLang = zzzz; m_TranslationsMap.insert(std::make_pair( std::make_pair(strIdCaption, strIdLang), pstTranslationData ) );
And finaly I write the code below to free memory used by this multimap:
m_TranslationsMap.erase(m_TranslationsMap.begin(), m_TranslationsMap.end());
can we say that memory is free ?
if not , what is the good way to do this ?
Thank you
|
|
|
|
|
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()
{
MyMap empty_map;
empty_map.swap(global_map);
}
|
|
|
|
|
Why mix std::string s and MFC CString s? And yes, the memory used to store the map's contents is freed since the map will handle destructing its contents and the contents don't require any manual cleanup.
Steve
modified 12-Oct-12 4:05am.
|
|
|
|
|
Well, I didn't assume the question is about freeing memory on destruction because that is trivial, however it might be the OP was curious about that...
|
|
|
|