-
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?
-
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]
-
thanks. i didn't think that it did.
-
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.
-
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.
-
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.