![]() |
|
#1
|
|||
|
|||
STL <list> removing elements from inside FOR loopI am getting a
debug assertion error: Expression: list iterator not incremental The list has 1 element and this error occurs at the line: iter = pLoaded->timerCalls.erase(iter); As I understand it, erase() is supposed to return the element after the one being erased, so this line SHOULD remove the single entry, and return list.end() I think. But obviously something is going wrong somewhere ? CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
Re: STL <list> removing elements from inside FOR loopQuote:
If it removes the last element, then the iterator is pointing beyond the end of the list. The for statement increments it before going to the top of the loop. I don't think that this is illegal, but maybe your compiler doesn't like it. Try the following: CPP / C++ / C Code:
Here's my output (using GNU g++ on a LInux platform: Code:
Now comment out the --iter at the end of the loop. My compiler still doesn't complain, but notice the output: Code:
Bottom line: I don't know if this is the cause of your compiler's complaint or what, but I have a feeling your code leaves something to be desired. Regards, Dave |
|
#3
|
|||
|
|||
Re: STL <list> removing elements from inside FOR loopSubtle but important type correction in my first post:
incremental = incrementable So I see the bug you meant, I was skipping ahead everytime I removed something since it would get incremented twice. Thanks. Using your example: With --iter Debug Assertion Failure Expression: list iterator not decrementable Withought --iter Same as your results. New test results: So I removed the .erase() call completely, and since I am (so far) just testing with 1 value in the list, I tried this method instead: CPP / C++ / C Code:
I am still getting debug assertion error: Expression: list iterator not incrementable I don't really get it, it should never even TRY to increment the iterator in this case no ? |
|
#4
|
|||
|
|||
Re: STL <list> removing elements from inside FOR loopActually, is list.End() supposed to be list.size()+1 ?
Maybe if I am doing clear() then size becomes 0, then the for loop tries to increment it again but now theres nothing in the list. Arg, I dunno |
|
#5
|
|||
|
|||
Re: STL <list> removing elements from inside FOR loopOk so, I got it wokring with a while loop instead
CPP / C++ / C Code:
If anyone is bored, and wants to explain the error of my previous ways, feel free, but don't feel you need to waste your time |
|
#6
|
|||
|
|||
Re: STL <list> removing elements from inside FOR loopQuote:
The fact is that my program had undefined behavior and just happened to work with that particular program and the particular compiler that I was using. In my program after deleting the very first element, the iterator is still pointing to intList.begin(). Decrementing it at that point is a no-no, and the program with your compiler bails out. (In my case, decrementing it and then incrementing it resulted in apparently satisfactory behavior, but it's still wrong.) Your solution is correct, and mine was incorrect. I usually try compiling programs like this with different compilers before posting, but I didn't have access to a Windows platform at the time. Another solution, in the same spirit as yours: CPP / C++ / C Code:
Output with various Windows and Linux/GNU compilers Code:
Next-to-bottom line: it's OK to increment or decrement a valid iterator as long as the result is valid. Decrementing an iterator that whose value is begin() is not OK. Bottom line: You can't prove a program is correct by testing alone. I am glad you got to the bottom of things in spite of my error. Thanks for following up. Regards, Dave |
|
#7
|
|||
|
|||
Re: STL <list> removing elements from inside FOR loopThanks for the assistance and the final clarification.
|
Recent GIDBlog
Welcome to Baghdad by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 21:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The