Connecting Tech Pros Worldwide Help | Site Map

iterator pointing to invalid container

  #1  
Old January 4th, 2007, 04:45 AM
Parapura Rajkumar
Guest
 
Posts: n/a

int main(int argc, char* argv[])
{
std::map<int, int>::iterator footest;
{
std::map<int, intfoo;
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

  #2  
Old January 4th, 2007, 05:55 AM
Victor Bazarov
Guest
 
Posts: n/a

re: iterator pointing to invalid container


Parapura Rajkumar wrote:
Quote:
int main(int argc, char* argv[])
{
std::map<int, int>::iterator footest;
{
std::map<int, intfoo;
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?
Quote:
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


  #3  
Old January 4th, 2007, 01:15 PM
Roland Pibinger
Guest
 
Posts: n/a

re: iterator pointing to invalid container


On Thu, 4 Jan 2007 00:47:13 -0500, "Victor Bazarov" wrote:
Quote:
>Parapura Rajkumar wrote:
Quote:
>int main(int argc, char* argv[])
>{
> std::map<int, int>::iterator footest;
> {
> std::map<int, intfoo;
> 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?
>
Quote:
>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/Confer...tions-2002.pdf

Best wishes,
Roland Pibinger
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
STL Vector Handle Mark answers 6 December 30th, 2007 11:15 AM
Q: change position of list element without invalidation Jakob Bieling answers 6 November 2nd, 2006 07:05 PM
A non-const std::set iterator Michael Klatt answers 26 July 22nd, 2005 10:05 AM
A non-const std::set iterator Michael Klatt answers 26 July 22nd, 2005 09:36 AM