Re: list.remove() crash.

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: James Ryan (___at__jryan@__icescape.__com)
Date: 05/14/04


Date: Fri, 14 May 2004 12:42:27 -0400

You are iterating through the list and deleting from it at the same time. I
suspect this will cause undefined behaviour. What are you trying to do?

James

"Polaris" <etpolaris@hotmail.com> wrote in message
news:uyvnAGXOEHA.2704@TK2MSFTNGP10.phx.gbl...
> Hi:
>
> I have a short code below which polulats a list with 3 strings and then
try
> to remove them by calling list.remove(). but the program crashes at the
2nd
> run in the loop (as marked below). Could you see anything I'm doing wrong?
>
> Thanks for your input.
> Polaris
> -------------------------------------------------------------
> #include <list>
>
> typedef std::list <char*>::iterator LI;
>
> int main (int argc, char* argv[])
> {
> char* array[] =
> {
> { "Test1" },
> { "Test2" },
> { "Test3" }
> };
>
> std::list <char*> mylist;
>
> for (int i=0; i<3; i++)
> mylist.push_back (array[i]);
>
> for (LI x=mylist.begin(); x!=mylist.end(); x++)
> mylist.remove (*x); // ******* crash at 2nd rund *****.
>
> return 0;
> }
>
>



Relevant Pages

  • Re: Remove items from a list
    ... Always a bad idea to modify the structure of a list ... (deleting or inserting items) ... while iterating through it, but it's so easy ... > Dan Perl wrote: ...
    (comp.lang.python)
  • Re: Having trouble deleting objects from a list...
    ... And if you modify self.objList while you're iterating over it, ... > involves creating a new list, but Python is supposed to optimize the ... > heck out of list comprehension, and removing items from a list in ... So deleting m items from an n-element list is ...
    (comp.lang.python)
  • Re: Paul Crankshaw fucks trolls
    ... wastelands of usenet? ... deleting the headers for this ng, for I suspect he does not want his ...
    (rec.sport.soccer)
  • Re: Iterate through array and delete if match
    ... Is this the correct way to delete matched entries from an array? ... deleting while iterating with each is undefined behavior. ...
    (comp.lang.ruby)
  • Re: Order of evaluation.
    ... > To be frank, neither do I! ... > or undefined behaviour. ... I suspect the latter, ... Took me ages to find that reference. ...
    (alt.comp.lang.learn.c-cpp)