Re: std::map - erase+continue

From: Andre Kostur (nntpspam_at_kostur.net)
Date: 03/09/05


Date: Wed, 09 Mar 2005 16:29:26 GMT

Peter Gordon <petergo@_deleteme_.netspace.net.au> wrote in
news:Xns9614C28EBB7BEpetergonetspacenetau@203.10.110.105:

> "Gernot Frisch" <Me@Privacy.net> wrote in news:397qcfF5uq1i4U1
> @individual.net:
>
>> Actually my question was: will pointers to other elements stay when I
>> remove/insert an element. (vector won't, list will keep pointers
>> valid)
>> -Gernot
>>
> Are you sure? In the following code fragment:
>
> list::iterator itr;
> list mylist;
> for(itr = mylist.begin(); itr != mylist.end(); ++itr) {
> // delete the last elemant in the list
> }
>
> What happens to the "itr != mylist.end()" test?

mylist.end() points to the one-past-the-end element. In this particular
case, it doesn't really matter since .end() is reevaluated every time you
go through the loop.

Nitpick, if you happen to be on the last element when you delete the last
element in the list, this loop will exhibit undefined behaviour since when
you delete the last element, the itr variable becomes an invalidated
iterator. Then you try to increment it, which is undefined behaviour.



Relevant Pages

  • Re: freeing allocated memory
    ... >>In the code fragment above d is a buffer allocated by a call to ... >>malloc in int2str and freed each pass through the loop. ... > This sounds very homework-like, so I'll just tell you that the ...
    (comp.lang.c)
  • Re: substring
    ... > of the array has been reached without encountering any null character. ... and yet dereferencing the other has undefined behaviour. ... while loop 3 does not. ...
    (comp.lang.c)
  • Re: Stack Exploit
    ... It invokes undefined behaviour as far as the C ... Here you modify a memory location which can "invoke undefined behavior" ... It doesn't loop when I compiled it here. ...
    (alt.lang.asm)
  • Re: K&R2, exercise 5.4
    ... contrary to your complaint) is that the inner string-comparison loop ... the loop continues merrily along until it finds somewhere they ... the end of the array - which is undefined behaviour. ...
    (comp.lang.c)
  • Re: Segmentation fault - interesting problem with array
    ... loop, the value of c1 went upto 16317. ... I have assigned only 255 to myimg i.e myimgThen Why is it so, it can go upto ... When you write off the end of an array you invoke undefined behaviour. ... There happened to be that much memory you could access without it causing symptoms that were visible to you. ...
    (comp.lang.c)