Munair wrote:BTW, speaking of optimization, GCC (or any other) shouldn't be trusted blind-fold with aggressive optimization. Here is a great example I accidentally found: https://oli4444.wordpress.com/2013/11/0 ... -for-loop/
Conclusion reads: "aggressive loop optimization that the [latest] gcc has (which then seems to remove the condition in the loop...)"
I cannot agree with your conclusion, sorry. The very first comment that the author of that blog got was:
Thomas W. wrote:It seems (from looking at the source code) that “characters” has 127 elements. You loop reaches 127 which accesses the 128th element which is invalid and results in undefined behavior.
There have been some changes to gcc (I think version 4.7) which result in weird behavior when it detects undefined cases.
If the code worked with a backwards loop, that should tell you that the problem lies in the iterator, not inside the loop. I have encountered this situation before of course (I work on 64-bit GCC with full optimization *always on*), but was usually able to track it to me just being stupid. If you do something like that, your code deserves to fail. In fact, that code didn't fail hard enough, in my opinion =D