Consider the following piece of code:
1 typedef std::deque<int> mydeque_t; 2 mydeque_t mydeque; 3 4 mydeque_t::iterator start = mydeque.begin(); 5 6 for (int i = 0; i != 1000; ++i) 7 mydeque.push_back(i); 8 9 for (mydeque_t::iterator myint = start; myint != mydeque.end(); ++myint) 10 *myint += 1;
When executing it I always get a runtime error on line 10 (live example: http://ideone.com/juQAA). However when I change line 6 to
for (int i = 0; i != 100; ++i) the code works fine.
The code would easily be fixed by moving the
start definition (line 4) behind the first for loop, but in my example I need it to stay before it. However even like this I think it should run fine, can anyone explain to me why it does not?