Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > iterator pointing to invalid container

Reply
Thread Tools

iterator pointing to invalid container

 
 
Parapura Rajkumar
Guest
Posts: n/a
 
      01-04-2007

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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-04-2007
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


 
Reply With Quote
 
 
 
 
Roland Pibinger
Guest
Posts: n/a
 
      01-04-2007
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/Confer...tions-2002.pdf

Best wishes,
Roland Pibinger
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find out a if an iterator is pointing to the very last (end) node? mailforpr@googlemail.com C++ 16 10-07-2006 05:53 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments