473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

map.erase, key cleanup

Can someone tell me whether or not my assumptions are good? Just want
to know if I'm going to run into trouble in how I'm deleting map
entries. Although I like to think I know my way around C++, I'm not an
STL expert by any stretch.

Here's a bit of code; notes on my assumptions follow:
_lookupMap is defined as:

map<string, long_lookupMap;

I'm adding to it something like this:

string * newKey = new string("Some Key Value");
_lookupMap[*newKey] = someIntegerValue;

....and I'm deleting an entry like this (no error checking, assume
find() does actually find...):

string origKey = "Some Key Value";
map<std::string, Int32>::iterator oldEntry = _lookupMap.find(origKey);
const string oldKey = oldEntry->first;
_lookupMap.erase( origKey );
I'm wondering... Will the map object cleanup the string key, or do I
need to delete it myself? Given that the string object is created on
the heap but dereferenced when used as a key, I would assume that the
call to erase will cleanup the string object itself, and I just need to
make sure there aren't any other pointers to the object left lying
around.

Do I have this right?

Remi.

Sep 12 '06 #1
6 3711
Rémi wrote:
Can someone tell me whether or not my assumptions are good? Just want
to know if I'm going to run into trouble in how I'm deleting map
entries. Although I like to think I know my way around C++, I'm not an
STL expert by any stretch.

Here's a bit of code; notes on my assumptions follow:

_lookupMap is defined as:

map<string, long_lookupMap;

I'm adding to it something like this:

string * newKey = new string("Some Key Value");
_lookupMap[*newKey] = someIntegerValue;
Unless you do:

delete newKey;

at some point before newKey goes out of scope, you have a memory leak.
A better way to do what it looks like you want to do is:

_lookupMap["Some Key Value"] = someIntegerValue;
...and I'm deleting an entry like this (no error checking, assume
find() does actually find...):

string origKey = "Some Key Value";
map<std::string, Int32>::iterator oldEntry = _lookupMap.find(origKey);
const string oldKey = oldEntry->first;
_lookupMap.erase( origKey );
I'm wondering... Will the map object cleanup the string key, or do I
need to delete it myself? Given that the string object is created on
the heap but dereferenced when used as a key, I would assume that the
call to erase will cleanup the string object itself, and I just need to
make sure there aren't any other pointers to the object left lying
around.
STL containers, including std::map, have copy semantics. What this
means is that values inserted into the containers are copied. So, your
code above allocates a new std::string, copies that std::string into the
map as a key, and then leaks the original std::string. The copied
std::string will be properly deallocated by the std::map, but it has no
relationship to the original string being passed in, other than having
an equal value.

Also, you should avoid using leading underscores in your identifiers,
because those are reserved for the compiler/standard library.

Hope this helps,
Nate
Sep 12 '06 #2
An enormous help, actually.
Thanks a lot, Nate.

Remi.

Nate Barney wrote:
Rémi wrote:
Can someone tell me whether or not my assumptions are good? Just want
to know if I'm going to run into trouble in how I'm deleting map
entries. Although I like to think I know my way around C++, I'm not an
STL expert by any stretch.

Here's a bit of code; notes on my assumptions follow:

_lookupMap is defined as:

map<string, long_lookupMap;

I'm adding to it something like this:

string * newKey = new string("Some Key Value");
_lookupMap[*newKey] = someIntegerValue;

Unless you do:

delete newKey;

at some point before newKey goes out of scope, you have a memory leak.
A better way to do what it looks like you want to do is:

_lookupMap["Some Key Value"] = someIntegerValue;
...and I'm deleting an entry like this (no error checking, assume
find() does actually find...):

string origKey = "Some Key Value";
map<std::string, Int32>::iterator oldEntry = _lookupMap.find(origKey);
const string oldKey = oldEntry->first;
_lookupMap.erase( origKey );
I'm wondering... Will the map object cleanup the string key, or do I
need to delete it myself? Given that the string object is created on
the heap but dereferenced when used as a key, I would assume that the
call to erase will cleanup the string object itself, and I just need to
make sure there aren't any other pointers to the object left lying
around.

STL containers, including std::map, have copy semantics. What this
means is that values inserted into the containers are copied. So, your
code above allocates a new std::string, copies that std::string into the
map as a key, and then leaks the original std::string. The copied
std::string will be properly deallocated by the std::map, but it has no
relationship to the original string being passed in, other than having
an equal value.

Also, you should avoid using leading underscores in your identifiers,
because those are reserved for the compiler/standard library.

Hope this helps,
Nate
Sep 12 '06 #3
STL containers, including std::map, have copy semantics. What this
means is that values inserted into the containers are copied. So, your
code above allocates a new std::string, copies that std::string into the
map as a key, and then leaks the original std::string. The copied
std::string will be properly deallocated by the std::map, but it has no
relationship to the original string being passed in, other than having
an equal value.
I know using pointers as keys isn't recommended, but would this imply
that if one were to use a pointer to an object as key type, you *would*
have to cleanup your key object as well?
>From what you've just said, I'd assume that the pointer address would
be copied upon insertion into the container, and that the pointer would
eventually get cleaned up, but the object at that location in memory
would leak?

Remi.

Sep 12 '06 #4
Rémi wrote:
>STL containers, including std::map, have copy semantics. What this
means is that values inserted into the containers are copied. So, your
code above allocates a new std::string, copies that std::string into the
map as a key, and then leaks the original std::string. The copied
std::string will be properly deallocated by the std::map, but it has no
relationship to the original string being passed in, other than having
an equal value.

I know using pointers as keys isn't recommended, but would this imply
that if one were to use a pointer to an object as key type, you *would*
have to cleanup your key object as well?
Correct.
From what you've just said, I'd assume that the pointer address would
be copied upon insertion into the container, and that the pointer would
eventually get cleaned up, but the object at that location in memory
would leak?
Again, correct, unless you call delete on the pointer first.

Nate
Sep 12 '06 #5
"Rémi" <re**@terracognita.cawrote:
Can someone tell me whether or not my assumptions are good? Just want
to know if I'm going to run into trouble in how I'm deleting map
entries. Although I like to think I know my way around C++, I'm not an
STL expert by any stretch.

Here's a bit of code; notes on my assumptions follow:
_lookupMap is defined as:

map<string, long_lookupMap;

I'm adding to it something like this:

string * newKey = new string("Some Key Value");
_lookupMap[*newKey] = someIntegerValue;
delete newKey;

You have to delete everything you new.
...and I'm deleting an entry like this (no error checking, assume
find() does actually find...):

string origKey = "Some Key Value";
map<std::string, Int32>::iterator oldEntry = _lookupMap.find(origKey);
const string oldKey = oldEntry->first;
_lookupMap.erase( origKey );
I'm wondering... Will the map object cleanup the string key, or do I
need to delete it myself? Given that the string object is created on
the heap but dereferenced when used as a key, I would assume that the
call to erase will cleanup the string object itself, and I just need to
make sure there aren't any other pointers to the object left lying
around.

Do I have this right?
You are working way too hard.

To add something to your lookupMap just do:

_lookupMap["Some Key Value"] = someIntegerValue;

To erase the entry...

_lookupMap.erase( "Some Key Value" );

--
There are two things that simply cannot be doubted. Logic and our
ability to sense the world around us. Doubt those, and you no longer
have anyone to discuss it with, nor any ability to discuss it.
Sep 12 '06 #6
Rémi wrote:
Can someone tell me whether or not my assumptions are good? Just want
to know if I'm going to run into trouble in how I'm deleting map
entries. Although I like to think I know my way around C++, I'm not an
STL expert by any stretch.

Here's a bit of code; notes on my assumptions follow:
_lookupMap is defined as:

map<string, long_lookupMap;

I'm adding to it something like this:

string * newKey = new string("Some Key Value");
_lookupMap[*newKey] = someIntegerValue;
map copies newKey object for store it. You don't need to create newKey
in the heap, and you are the owner of newKey (not _lookupMap). Then you
must delete newKey.
>
...and I'm deleting an entry like this (no error checking, assume
find() does actually find...):

string origKey = "Some Key Value";
map<std::string, Int32>::iterator oldEntry = _lookupMap.find(origKey);
const string oldKey = oldEntry->first;
_lookupMap.erase( origKey );
I'm wondering... Will the map object cleanup the string key, or do I
need to delete it myself? Given that the string object is created on
the heap but dereferenced when used as a key, I would assume that the
call to erase will cleanup the string object itself, and I just need to
make sure there aren't any other pointers to the object left lying
around.
Call to erase will cleanup the string inside the map, not the one you
have created (newKey).
>
Do I have this right?

Remi.
Sep 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Angus Leeming | last post by:
Dinkumware's online STL reference http://tinyurl.com/3es52 declares std::map's overloaded erase member functions to have the interface: map::erase iterator erase(iterator where); iterator...
7
by: jose luis fernandez diaz | last post by:
Hi, Is this right any stl container (vector, deque, list, . . .)? typedef vector container; int main() { container<int> c1;
3
by: jose luis fernandez diaz | last post by:
Hi, Erase elements while iterating on a map don't invalidate the iterator except the erased one, so the program below: (1) #include <map> int main()
20
by: Tom van Stiphout | last post by:
I'm about to write a function like below, which I'm going to call a lot of times. So I care about possible memory leaks. I think whether I should use Erase or not depends on whether Split creates...
8
by: olanglois | last post by:
Hi, I was asking myself to following question. What is better to erase an element from a STL map: calling (option #1) size_type erase(const key_type& k) or calling (option #2)
11
by: moleskyca1 | last post by:
Hi, I know if you call erase when you iterate through map you will crash. Ex: map<int,doublem; // insert something for ( map<int, double>::iterator i = m.begin(); i != m.end(); i++ ) if (...
69
by: MQ | last post by:
Hi all I am just wondering how most people implement cleanup in C functions. In particular, if the function opens a number of resources, these need to be released properly should an error occur...
6
by: catphive.lists | last post by:
Is there a way to call erase(iter) on a list without invalidating the iterator? Can I make a copy of an iterator and then move forward the original without moving the copy? I'm aware of the...
3
by: subramanian100in | last post by:
Consider vector<stringv; If we call, v.erase(v.end()) this invokes undefined behaviour. But, if we call
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.