iterator pointing to invalid container

Discussion in 'C++' started by Parapura Rajkumar, Jan 4, 2007.

  1. int main(int argc, char* argv[])
    {
    std::map<int, int>::iterator footest;
    {
    std::map<int, int> foo;
    foo[0] = 0;
    footest = foo.begin();
    }
    return 0;
    }

    Is this undefined behavior? Please note that footest is going out of
    scope before std::map. In theory an implementation could have a
    iterator destructor that could still access the std::map ?

    Thanks
    Raj
     
    Parapura Rajkumar, Jan 4, 2007
    #1
    1. Advertising

  2. Parapura Rajkumar wrote:
    > int main(int argc, char* argv[])
    > {
    > std::map<int, int>::iterator footest;
    > {
    > std::map<int, int> foo;
    > foo[0] = 0;
    > footest = foo.begin();
    > }
    > return 0;
    > }
    >
    > Is this undefined behavior? Please note that footest is going out of
    > scope before std::map.


    You mean, "after 'foo'", don't you?

    > In theory an implementation could have a
    > iterator destructor that could still access the std::map ?


    No. The iterator is allowed to be invalid, that does not affect
    its destruction.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 4, 2007
    #2
    1. Advertising

  3. On Thu, 4 Jan 2007 00:47:13 -0500, "Victor Bazarov" wrote:
    >Parapura Rajkumar wrote:
    >> int main(int argc, char* argv[])
    >> {
    >> std::map<int, int>::iterator footest;
    >> {
    >> std::map<int, int> foo;
    >> foo[0] = 0;
    >> footest = foo.begin();
    >> }
    >> return 0;
    >> }
    >>
    >> Is this undefined behavior? Please note that footest is going out of
    >> scope before std::map.

    >
    >You mean, "after 'foo'", don't you?
    >
    >> In theory an implementation could have a
    >> iterator destructor that could still access the std::map ?

    >
    >No. The iterator is allowed to be invalid, that does not affect
    >its destruction.


    Different kinds of invalid iterators are described here:
    http://www.angelikalanger.com/Conferences/Slides/CppInvalidIterators-DevConnections-2002.pdf

    Best wishes,
    Roland Pibinger
     
    Roland Pibinger, Jan 4, 2007
    #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. Hendrik Maryns
    Replies:
    18
    Views:
    1,468
  2. greg
    Replies:
    6
    Views:
    483
    Dietmar Kuehl
    Jul 17, 2003
  3. Vivi Orunitia
    Replies:
    11
    Views:
    4,545
    Martijn Lievaart
    Feb 4, 2004
  4. Replies:
    6
    Views:
    698
    Jim Langston
    Oct 30, 2005
  5. Replies:
    16
    Views:
    610
    Jim Langston
    Oct 7, 2006
Loading...

Share This Page