sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Parapura  Rajkumar's Avatar

iterator pointing to invalid container


Question posted by: Parapura Rajkumar (Guest) on January 4th, 2007 04:45 AM

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 Answers Posted
Victor Bazarov's Avatar
Guest - n/a Posts
#2: Re: iterator pointing to invalid container

Parapura Rajkumar wrote:
Quote:
Originally Posted by
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:
Originally Posted by
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


Roland Pibinger's Avatar
Roland Pibinger January 4th, 2007 01:15 PM
Guest - n/a Posts
#3: Re: iterator pointing to invalid container

On Thu, 4 Jan 2007 00:47:13 -0500, "Victor Bazarov" wrote:
Quote:
Originally Posted by
>Parapura Rajkumar wrote:
Quote:
Originally Posted by
>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:
Originally Posted by
>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/Confe...ctions-2002.pdf

Best wishes,
Roland Pibinger
 
Not the answer you were looking for? Post your question . . .
197,004 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,004 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors