a stl map which use stl pair as the key

A

Allerdyce.John

I have a a stl map which use stl pair as the key

class A;

type map< pair<int, int>, A> MyMap;

if I have function which add entry to the map:
void func(int x, int y, MyMap& map, A& a) {
pair<int, int> key(x, y);
map[key] = a;
}

My question is what should i do to free the pair key of the map to
ensure there is no memory leak?

Thank you.
 
C

Clem.Dickey

I have a a stl map which use stl pair as the key ....
My question is what should i do to free the pair key of the map to
ensure there is no memory leak?

You don't need to do anything. I'm not sure where you expect a memory
leak, but there is none. The key you constructed will be deleted as
when the functon returns. The key in the map (a copy of the key you
constructed) must and will exist as long as the key/value pair is in
the map.

As a general rule, memory leaks exist only where you see the "new"
operator without a corresponding "delete." In the case of map (and the
rest of STL) the library is responsible for any new, and any
corresponding delete.
 
K

klaus hoffmann

I have a a stl map which use stl pair as the key

class A;

type map< pair<int, int>, A> MyMap;

if I have function which add entry to the map:
void func(int x, int y, MyMap& map, A& a) {
pair<int, int> key(x, y);

this pair is "automatic", i.e. created on the stack and will be "freed"
at the closing }

map[key] = a;

This statement creates a copy of a and puts it in the map. It will be
freed when the map is freed
}

My question is what should i do to free the pair key of the map to
ensure there is no memory leak?

Thank you.

hth
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top