CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2012
    Posts
    4

    Deleting this particular std::map

    Dear All,

    could you kindly help me with deleting this std::map. Here are some details...

    typedef std::map<std::string,int> N2v;
    N2v n2v;

    //Some looping construct
    n2v.insert(....)
    //End of some looping construct

    //Now for the delete :
    for(int i=0;i<n2v.size();i++){
    // std::map<int>.swap(n2v[i]); -> IS WRONG. KINDLY GUIDE ME IN FIXING IT
    }
    std::map<std::string,int>().swap(n2v);

    Thanks,
    Ravi

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Deleting this particular std::map

    What do you mean by "deleting"?
    If it is something like "erasingall the elements of a map" then use clear().
    See map Members
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Deleting this particular std::map

    Quote Originally Posted by sunshekar View Post
    Dear All,

    could you kindly help me with deleting this std::map. Here are some details...
    What's wrong with just letting the destructor of the map do its job?
    Code:
    typedef std::map<std::string,int> N2v;
    
    void foo()
    {
        // some code
       {
            N2v n2v;
            //End of some looping construct
        }  // N2v is now destroyed
    }
    The N2v map is deleted once the end of the local block is reached. There is no need to do any deletions yourself.

    Regards,

    Paul McKenzie

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Deleting this particular std::map

    Why are you using swap()? You appear to be trying to use the "swap trick", but that is for vectors. With a map, you can just call .clear().

  5. #5
    Join Date
    Feb 2012
    Posts
    4

    Re: Deleting this particular std::map

    Hi all,

    some clarifications:

    1. By deleting, I mean removing from memory. We have observed that a simple clear does not release the memory for 2D vectors (unlike 1D vectors) , even when it goes out of scope. Hence the need for this delete i.e. releasing it from memory.
    2. "Why are you using swap()? You appear to be trying to use the "swap trick", but that is for vectors. With a map, you can just call .clear().": Are you sure? In 2D vectors, the memory need not be released at the end of scope (unlike 1D vectors).

    Thanks,
    Ravi

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Deleting this particular std::map

    Quote Originally Posted by sunshekar View Post
    ... We have observed that a simple clear does not release the memory for 2D vectors (unlike 1D vectors) , even when it goes out of scope. Hence the need for this delete i.e. releasing it from memory.i
    Could you describe the way you "have observed that a simple clear does not release the memory for 2D vectors (unlike 1D vectors) , even when it goes out of scope"?
    And please, define "does not release the memory".
    Victor Nijegorodov

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Deleting this particular std::map

    Quote Originally Posted by sunshekar View Post
    Hi all,

    some clarifications:

    1. By deleting, I mean removing from memory. We have observed that a simple clear does not release the memory for 2D vectors
    You are using a std::map, not vectors.
    Are you sure? In 2D vectors, the memory need not be released at the end of scope (unlike 1D vectors).
    And all of that coding could be a big waste of time. All you're doing by coding "delete loops" is just wasting cycles that accomplish nothing.

    Once the map goes out of scope, anything else after that is dependent on the heap manager and OS. The map has done its job by deallocating its internal structure. After that the heap manager is responsible to see if the memory is returned back to the OS, or keep it for later allocations -- it isn't up to you to decide this or even the map class (unless you code a custom allocator and call Win32 heap functions to allocate/deallocate memory).

    The question is this: why your application needs to do any of this?

    Regards.

    Paul McKenzie
    Last edited by Paul McKenzie; February 2nd, 2012 at 12:03 PM.

  8. #8
    Join Date
    Feb 2012
    Posts
    4

    Re: Deleting this particular std::map

    For instance,

    inside a function....

    int Nproc = 2000;
    std::vector<std::vector<double>> test(Nproc);

    for(int i=0;i<Nproc;i++){
    for(int j=0;j<1000;j++){
    test[i].push_back( (j*1.0));
    }
    }

    //End of function

    The compilers nowadays may not deallocate this test vector immediately even after it goes afer scope. They are sort of "optimised" to dellocate them in some unknown way. This can be verified in large production type codes, by using the "top" command in linux. One can see that the memory is not automatically deallocated even after the routine is over (for 2D vectors).

    By releasing the memory, I mean deallocating the memory.

    Warm Regards,
    Ravi

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Deleting this particular std::map

    Quote Originally Posted by sunshekar View Post
    Hi all,

    some clarifications:

    1. By deleting, I mean removing from memory. We have observed that a simple clear does not release the memory for 2D vectors (unlike 1D vectors)
    How are you defining a 2D vector? If it's a vector<vector<T>>, then the destructors of the inner vector objects ought to be called when the outer vector is cleared (even if the memory they're sitting in isn't released), unless you're using a vector class which has some kind of special optimization that keeps them around.

  10. #10
    Join Date
    Feb 2012
    Posts
    4

    Re: Deleting this particular std::map

    Defined as given in my earlier post. All the entities are doubles, not any special class.

    Nevertheless, based on our experiences doing this swap() is the only way we can be certain that the memory is deallocated.

    Warm Regards,
    Ravi

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Deleting this particular std::map

    Quote Originally Posted by sunshekar View Post
    For instance,
    You need to use code tags when posting code.
    Code:
    int Nproc = 2000;
    std::vector<std::vector<double>> test(Nproc);
    
    for(int i=0;i<Nproc;i++){ 
      for(int j=0;j<1000;j++){
        test[i].push_back( (j*1.0));
      }
    }
    See how that looks?

    Now:
    The compilers nowadays may not deallocate this test vector immediately even after it goes afer scope.
    The compiler does deallocate memory, else you have a memory leak. What you want to do goes way beyond what the compiler is doing.

    Read my previous post. Control of whether deallocated memory goes back to the OS is not the job of the compiler. That job goes to the CRT heap manager.

    The vector class isn't magic. It isn't doing anything that isn't C++. A vector class uses the same new/delete calls as your own code would do. So the issue you're seeing is not a vector or a map issue. It is an issue with the heap manager and how it controls memory.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Deleting this particular std::map

    Quote Originally Posted by sunshekar View Post
    Defined as given in my earlier post. All the entities are doubles, not any special class.

    Nevertheless, based on our experiences doing this swap() is the only way we can be certain that the memory is deallocated.
    All the swap() does is make sure the memory is released while the vector is still in scope. If you have two vectors in scope, and one of them is not needed anymore, then the swap trick has merit.

    Once that vector goes out of scope, then the "go out of scope" becomes the ultimate "swap trick", as any and all calls to delete the vector's contents are issued via the vector's destructor.

    Again, a vector isn't magic -- it isn't coded with another language that somehow knows to call the OS to return the memory, and cannot do things beyond what the C++ language provides. A vector is no different than any class you would write. It is calling new, delete, etc. just like you would call new and delete. Yes, a vector may be complex, but still, it's just good old C++.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured