how to clear stl nested map

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • onunix
    New Member
    • May 2013
    • 3

    how to clear stl nested map

    here is a nested stl map

    Code:
    typedef struct struct_RISCP
    	{
    		
    
    		float nSCPTotal;
    		int nSCPCount;
    		CString strIMEI;
    		
    		std::map<UINT8, int> mapSection;
    		
    
    	}RISCPSt;
    map<stRncCellIdntyDmnType, RISCPSt>m_mapRISCP;
    it occupied too much memory,i wanted to clear them(both outer map and inner map) ,some one told me you just need to call m_mapRISCP.clea r(),then the mapSection (inner map) will be cleared automaticly,in other words, m_mapRISCP.clea r() will clear both outer map and inner map,who can tell me is it right, thanks.
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    onunix,

    You should be good.

    Since I can't quote chapter and verse of the C++ specification at you, I looked in my system's include files.

    I looked in the STL implementation of map, which is implemented as a red/black tree. Looking at the tree object, I see that the "clear" method is explicity called in its destructor.

    Cheers,
    Oralloy

    Comment

    • onunix
      New Member
      • May 2013
      • 3

      #3
      I used visual studio 2010,according to what you say,inner map ( mapSection) 's destruction will be called,then what do the destruction do ? will it clear the memory occupied by the element of mapSection ?

      Comment

      • onunix
        New Member
        • May 2013
        • 3

        #4
        if the element of mapSection(inne r map) is cleared automaticly as the outer map's clear() method is called, It seems that it 's not cleared at once, maybe like java garbage collection, it's cleared later. who can make sure it for me,thanks.

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          onunix,

          Do your own work.

          The comprehensive way to verify that you are getting the destruction done is to create your own shadow new and delete (malloc and free), and then track the addresses. I've done this in the past, it's a bit of effort, and you will have a great deal of data to dig through, assuming your application processes a lot of memory objects.

          Alternately, you can put a destructor in your struct_RISCP object, and verify that you are getting the correct destruction of the map. Better yet, you can force clearing of the contained map, and obviate your question.

          Alternately, you might want to look through your visual studio header files and verify that you are actually getting the desired behaviour.

          Sorry for being so short, I have a bit of a headache this morning. If you honestly believe that the nested maps are not being cleared, then you will have to do a few experiments - I suggest a small sandbox program, as experimenting in a large project is usually painful.

          Another techniques you can use for diagnosing your problem is to sub-class the nested map, and then override the clear method. If it is called, you are in luck.

          As for what clear does, it returns the memory comprising the contents of the map's structure to the memory pool - it does not normally erase, or otherwise destroy the content of the memory returned.

          Kind Regards,
          Oralloy

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            onunix,

            By the way - if you really want to destroy the contents of the map's memory, then you have a different sort of problem.

            In this case, I do not see any reason to destroy the memory contents - all you have is a map emulating a small, sparse integer array, with integer values.

            If you expect all 256 key values (the type is UINT8) to be used, then you may be better off using an array instead of a map.

            Oralloy

            Comment

            Working...