I came across a strange typo in one of our C++ applications the other day that triggered the code to enter an infinite loop. At the end of a for loop, instead of just incrementing i++, the programmer accidentally assigned i = i++.
for (int i = 0; i < 10; i = i++) {
cout << "i = " << i << endl;
}
output:
i = 0
i = 0
i = 0
...
The strangest part of this, is that even understanding that this was a typo, I cannot see why this code would not work. As far as I can tell, assigning to i should have no impact because i should still be incremented after the assignment. (For some reason it must instead be incrementing a copy? That doesn't seem to make sense though.)
Another interesting thing to note is changing the assignment to i = ++i will not produce an infinite loop.
This code did not produce an infinite loop on our previous compiler: gcc version 4.4.7 20120313. We recently upgraded to gcc version 4.8.5 20150623 and now it does.
The question is why does this code produce an infinite loop when logically it looks like it shouldn't, and which compiler is interpreting this correctly according to the C++ standard?