iterator pointing to invalid container

  • Thread starter Parapura Rajkumar
  • Start date
P

Parapura Rajkumar

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
 
V

Victor Bazarov

Parapura said:
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
 
R

Roland Pibinger

Parapura said:
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
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top