Thread: does map create a permanent copy of pointers to temporary variables?

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    consider:

    Code:
    //---------------------------------------------------------------------------
    #include <map>
    #pragma hdrstop
    
    //---------------------------------------------------------------------------
    
    #pragma argsused
    class foo
    {
            std::map<int,int*>m;
            public:
    
            foo()
            {
                    int i=1;
                    m[0]=&i;
            }
            int func(int key)
            {
                    return *m[key];
            }
    };
    
    
    int main(int argc, char* argv[])
    {
            foo f;
            int i = f.func(0);
    }

    this "seems to work"; but i'm afraid this is just because that temporary's location just still happens to be valid because it hasn't been overwritten or some such.

    the documentation for map doesn't appear to specify this aspect of its behavior.

    anyone happen to know if it makes an internal permanent copy?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, it does not. If you think about it, it wouldn't make sense for map to do so: what would then happen when you added dynamically allocated variables to a map, for example? Would you be responsible for deleting the object, or would the map? [edit=2] I mean the original object.

    And if a copy of the object were made, that would also make the map pretty inefficient. Say you had a huge dynamically allocated object that you wanted to add to a map -- you wouldn't want the map creating another giant copy of it, would you?
    [/edit]

    BTW, you can test this by storing an object like this in a map.
    Code:
    class CopyNotify {
    public:
        CopyNotify(const CopyNotify &other) { cout << "Copy made\n"; }
        CopyNotify &operator = (const CopyNotify &other)
            { cout << "Copy made\n"; return *this; }
    };
    [edit] Note that if you do store, for example, dynamically allocated ints in a map, it's your responsibility to free those ints -- which is possible, you just have to step through the map -- it's just annoying. [/edit]
    Last edited by dwks; 12-08-2008 at 09:30 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    thanks. i didn't think that it did.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Boost has a ptr_container library that has a bunch of containers that hold and manage dynamically allocated memory instead of regular objects. This is useful if you want to store pointers in the container (to use with polymorphism for example). Check it out to see if there is a ptr_map that could make your life a lot simpler.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    ok now you're piqued my curiousity.

    i store polymorphic descendants in maps as pointers to their base classes all the time without any problems.

    what do you mean by "this is useful ... to store pointers in the container [to use with polymorphism]"?

    my life isn't tough here; std::map actually gives me the behaviour i want, i just was taken aback when i realized i had stored a pointer to a temporary variable in the map and i didn't get a error when it was called up later. i just wanted to make sure i wasn't creating a memory leak.
    Last edited by m37h0d; 12-09-2008 at 01:45 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I assume you manage the dynamic memory yourself, right? You call new and you call delete, right? Assuming the map is a member of a class, you wrote the destructor of the class and (hopefully) made sure the copy functions were disabled or implemented properly, right?

    If you've done all that work and it is working correctly for you, then great. I've done the same thing myself. However, if you use ptr_map, then all that work (except for the call to new) is done for you. And it is likely that it has been done correctly.

    That's how such a tool can make your life a lot simpler. You can use ptr_map and worry only about following its interface (which should be rather straightforward) rather than worrying about all the different aspects of manual memory management.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to create an array of pointers to files
    By alavardi in forum C Programming
    Replies: 6
    Last Post: 03-05-2005, 09:35 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Graphing a direction field and intensity map
    By InnerCityBlues in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2004, 10:59 PM
  4. How to copy array without create a new one
    By thone in forum C Programming
    Replies: 11
    Last Post: 08-02-2004, 01:24 AM
  5. Replies: 5
    Last Post: 11-20-2001, 12:48 PM