Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > a stl map which use stl pair as the key

Reply
Thread Tools

a stl map which use stl pair as the key

 
 
Allerdyce.John@gmail.com
Guest
Posts: n/a
 
      02-22-2006
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.

 
Reply With Quote
 
 
 
 
Clem.Dickey@gmail.com
Guest
Posts: n/a
 
      02-22-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.

 
Reply With Quote
 
 
 
 
klaus hoffmann
Guest
Posts: n/a
 
      02-22-2006
(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stl map: get the <key,value> pair which has the minimum value Rui Maciel C++ 2 12-01-2009 11:21 PM
map with pair as key kietzi@web.de C++ 11 06-21-2006 07:14 AM
map with pair of key Ian Collins C++ 4 05-10-2006 12:20 PM
map.insert(key,val) vs. map[key]=val ? Patrick Guio C++ 6 10-20-2004 01:54 PM



Advertisments