2

New to cpp. When I call to erase on iterator without increment it my program exit immediately without any sign or crash. In the following example only 'After if condition ' is printed and than nothing. no error displayed. however if i do the correct call to list.erase(it++); than it print all. my question is about what happened when I don't call it correct. why i don't see any crash or exit? My worried and the reason that i asked the question is about catching these kind of crashes, why i didn't catch it in the catch block? is there a way to catch it?

int main(int argc, char *argv[]) {
    try {
        list<int>::iterator it;
        list<int> list;
        list.push_back(1);
        for (it = list.begin(); it != list.end(); ++it) {
            if ((*it) == 1) {
                list.erase(it);
            }
            cerr << "After if condition " << endl;
        }
        cerr << "After for condition" << endl;
    } catch (...) {
        cerr << "catch exception" << endl;
    }
    cerr << "Finish" << endl;
}
Avihai Marchiano
  • 3,837
  • 3
  • 38
  • 55
  • 1
    Duplicate of http://stackoverflow.com/questions/3866642/stl-list-erase-items – steffen Jul 25 '12 at 12:33
  • Guys , the question is not how to do it correct . i know how to do it correct. my question is what happened to the program when i dont do it correct. why there is no crash? – Avihai Marchiano Jul 25 '12 at 12:36
  • You're most likely getting a crash whether or not you see it. Using an erased iterator means you're trying to iterate off of something that doesn't exist, invoking undefined behavior. The behavior is LITERALLY undefined, so your question about "where does it go" is impossible to answer. You crashed though from the sounds of it - most likely a segmentation fault / SIG11. – John Humphreys Jul 25 '12 at 12:44
  • My worried and the reason that i asked the question is about catching these kind of crashes, why i didn't catch it in the catch block? is there a way to catch it? – Avihai Marchiano Jul 25 '12 at 12:46
  • 1
    You cannot catch signals, it's impossible - they are not exceptions. When you reference memory and you happen to hit something not owned by your program, the operating system signals your program (segmentation fault/sig11) as it is operating out of its bounds. This isn't a language construct, it's an operating system protection scheme independent of language. You just don't see it in languages like C#/Java/etc because they protect you from it by handling your pointers, etc. for you. C/C++ are low level languages that let you manage your own pointers/memory so you can get these errors. – John Humphreys Jul 25 '12 at 12:50
  • Thank you - w00te . This is what i need to learn. – Avihai Marchiano Jul 25 '12 at 12:57

4 Answers4

6

From std::list::erase():

References and iterators to the erased elements are invalidated.

it is erased, and then used by ++it causing undefined behaviour: meaning anything can happen.

erase() returns the iterator following the last removed element, meaning you can store the return value of erase() and continue but the loop requires modification to avoid skipping an element after an erase() has been executed.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • 2
    @user1495181, see my answer: _undefined behaviour_. – hmjd Jul 25 '12 at 12:41
  • Undefined behavior is literally undefined @user1495181, so your question isn't answerable. Sounds like you crashed though which is definitely a likely outcome. – John Humphreys Jul 25 '12 at 12:46
2

You may not alter the container when using iterators. erase invalidates the iterator immediatley.

What you can do is something like:

for( list<int>::iterator it = list.begin(); it != list.end();)
  if( (*it) == 1 )        
    it=list.erase(it);
  else
    ++it;

(from STL list erase items)

or

for( list<int>::iterator it = list.begin(); it != list.end();)
  if( (*it) == 1 )        
    list.erase(it++);
  else
    ++it;

There are many posts on that. Search for "erase list iterator"!

edit: If you do otherwise, i.e. invalidate the iterator and increasi afterwards, the behaviour is undefined. That means it can differ from system to system, compiler to compiler, run to run.

If it doesn't crash, and gives proper behaviour, you're lucky. If it doesn't crash and does something else, you're unlucky, because it's a hard-to-find bug.

Community
  • 1
  • 1
steffen
  • 8,572
  • 11
  • 52
  • 90
2

You can not use it once you erase it. Calling ++it causes the crash. Store the next iterator before erasing the element.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

The trick is to use erase(it++).

ritter
  • 7,447
  • 7
  • 51
  • 84