Discussion:
Map keys and dynamic data
(too old to reply)
Joel Olson
2005-12-11 17:52:25 UTC
Permalink
For the case of "map< char*, someobject*, ltstr > amap;

where ltstr is used to compare two c strings. If the following code is
executed

...
char* mapkey = new char[ SOMECONST ];
strcpy( mapkey, "akey" );
someobject* someobj = new someobject();
// Initialize someobj

amap[ mapkey ] = someobj;
...


We have to remember that mapkey was used and delete it ourselves
correct? Meaning amap.erase( "akey" ) will only remove the map entry
not the memory for the key or the object held in the map entry, right?

Thanks,
Joel
Todd Harrington
2005-12-11 18:11:38 UTC
Permalink
Joel,

You can do delete [] (amap.find(mapkey))->first;

and that will delete the char* that is used as the key.

-Todd
Post by Joel Olson
For the case of "map< char*, someobject*, ltstr > amap;
where ltstr is used to compare two c strings. If the following code is
executed
...
char* mapkey = new char[ SOMECONST ];
strcpy( mapkey, "akey" );
someobject* someobj = new someobject();
// Initialize someobj
amap[ mapkey ] = someobj;
...
We have to remember that mapkey was used and delete it ourselves correct?
Meaning amap.erase( "akey" ) will only remove the map entry not the memory
for the key or the object held in the map entry, right?
Thanks,
Joel
Joel Olson
2005-12-11 19:46:29 UTC
Permalink
Thanks
Post by Todd Harrington
Joel,
You can do delete [] (amap.find(mapkey))->first;
and that will delete the char* that is used as the key.
-Todd
Post by Joel Olson
For the case of "map< char*, someobject*, ltstr > amap;
where ltstr is used to compare two c strings. If the following code is
executed
...
char* mapkey = new char[ SOMECONST ];
strcpy( mapkey, "akey" );
someobject* someobj = new someobject();
// Initialize someobj
amap[ mapkey ] = someobj;
...
We have to remember that mapkey was used and delete it ourselves correct?
Meaning amap.erase( "akey" ) will only remove the map entry not the memory
for the key or the object held in the map entry, right?
Thanks,
Joel
Loading...