Thread: another map question

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838

    another map question

    can you safely define a reference to a value stored in map, or can adding elements to a map alter the addresses of already stored values?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by m37h0d View Post
    can you safely define a reference to a value stored in map, or can adding elements to a map alter the addresses of already stored values?
    The basic algorithm for map doesn't "need" to change the address of already stored elements, but it may do so anyways. I wouldn't rely on them having the same address in production code...

    If you actually want to do that, you may want to store a pointer (for example a std::tr1::shared_ptr), and copy the pointer into some other variable - obviously, map will not change where your user-data is from a passed in pointer - it will just store the pointer together with your key.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    As far as I know, none of the node-based containers ever reallocate a node, once it is inserted and hasn't been removed (because the container can be reordered by changing the links).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by m37h0d
    can you safely define a reference to a value stored in map, or can adding elements to a map alter the addresses of already stored values?
    To quote section 23.1.2 of the 2003 edition of the C++ standard:
    The insert members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by matsp View Post
    The basic algorithm for map doesn't "need" to change the address of already stored elements, but it may do so anyways. I wouldn't rely on them having the same address in production code...

    If you actually want to do that, you may want to store a pointer (for example a std::tr1::shared_ptr), and copy the pointer into some other variable - obviously, map will not change where your user-data is from a passed in pointer - it will just store the pointer together with your key.

    --
    Mats
    that's what i was thinking - i couldn't see any reason why it would need to change the addresses, but i didn't want to bet on it without knowing for sure.

    i already opted to go for storing a pointer. it meant i no longer had a trivial destructor, but that's no big deal.

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by laserlight View Post
    To quote section 23.1.2 of the 2003 edition of the C++ standard:
    excellent. thank you. i suppose i should find where that standard is located and google it myself when i have STL questions.

    it is a bit more elegant when i dont' have to use the pointers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding a Map to a program
    By Shogun32 in forum C++ Programming
    Replies: 1
    Last Post: 05-04-2009, 09:42 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  4. Creating a map of objects with variable width and height
    By MrSparky in forum C++ Programming
    Replies: 6
    Last Post: 07-30-2007, 03:06 PM
  5. Keys of a map
    By nickname_changed in forum C++ Programming
    Replies: 4
    Last Post: 07-10-2003, 04:46 AM