a stl map which use stl pair as the key

Discussion in 'C++' started by Allerdyce.John@gmail.com, Feb 22, 2006.

  1. Guest

    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.
    , Feb 22, 2006
    #1
    1. Advertising

  2. Guest

    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.
    , Feb 22, 2006
    #2
    1. Advertising

  3. 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
    klaus hoffmann, Feb 22, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Guio
    Replies:
    6
    Views:
    3,180
    chris
    Oct 20, 2004
  2. Ian Collins

    map with pair of key

    Ian Collins, May 10, 2006, in forum: C++
    Replies:
    4
    Views:
    485
  3. Replies:
    11
    Views:
    797
    silversurfer
    Jun 21, 2006
  4. Replies:
    1
    Views:
    545
    Daniel Pitts
    Nov 16, 2007
  5. Rui Maciel
    Replies:
    2
    Views:
    2,986
    AndrewDover
    Dec 1, 2009
Loading...

Share This Page