CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2011
    Posts
    1

    Accessing iterator past the end of a list

    Hi all,
    I'm trying to understand the code at the bottom of my post. I am working with a someone else's code and don't have a lot of experience in C++. My problem is that in the second loop it is accessing a field in an object pointed to by the first iterator. However, surely this iterator is now equal to the end of the array as it is the exit condition of the first loop.

    Would someone be able to explain to me how this is working?

    Any help would be much appreciated. Thanks.

    Code:
    for(iter=M.mappings.begin(); iter!=M.mappings.end(); iter++)	{
    	another.erase(iter->target);
    	s<< "\t\tdb"<<iter->target<<" [label=\""<<G2->getNodeAttrs(iter>source)->find("name")->second.value<<"\" shape=box style=filled fillcolor=lightblue color=blue];\n";		
    }
    		
    for(p1=another.begin(); p1!=another.end(); p1++)
    {
    	s<< "\t\tdb"<< (*p1)<<" [label=\""<<G2->getNodeAttrs(iter->source)- >find("name")->second.value<<"\" shape=box];\n";

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Accessing iterator past the end of a list

    Indeed, that seems like an error. Note also this bit: "G2->getNodeAttrs(iter>source)" seems to be saying something other than it intends as well.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Accessing iterator past the end of a list

    Quote Originally Posted by Brick86 View Post
    Would someone be able to explain to me how this is working?
    When a mistake such as this is made in a C++ program, nothing guarantees that the application will behave in a strange manner. Therefore the program seems to "work", but internally it has a bug.

    Having said that, many compilers will check iterator validity when compiled with "debugging" turned on.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Apr 2008
    Posts
    118

    Re: Accessing iterator past the end of a list

    For a long time, the MS compiler that shipped with VS would happily let you do things you shouldn't with off-the-end iterators. As I recall, under the hood, iterators were being implemented to some degree as pointers, so you could quite happily read invalid data and be none the wiser.

    I wouldn't be surprised if other compilers of the era exhibited similar oddities

  5. #5
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Accessing iterator past the end of a list

    Quote Originally Posted by Moschops View Post
    As I recall, under the hood, iterators were being implemented to some degree as pointers
    Not only to some degree, but they simply were typedefs of a pointer type, e. g. std::vector<T>::iterator of Visual C++ 6.

    But here we seem to have a std::map and that could not be done by a simple pointer. So I would assume the 'another' was empty when reaching the second loop and hence it didn't crash.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured