Visual C++ 2008 bug?
c++, visual-studio-2008
Solution
I can repro that on VS2008 SP1. It is a code optimizer bug, as usual. You'd have to look at the disassembly to see the cause. It fumbles at `2*a <= 7` when it factors out the multiplication, it generates code for `a <= 2`. That's wrong of course, should have been `a <= 3` or `a < 4`. Looks like it doesn't handle the <= operator correctly for divisions. Kinda tricky, it has to know the difference between odd and even numbers :)
The bug disappears when you don't force it to figure out how <= behaves with division, using `2*a < 8` works fine.
This bug has been fixed a while ago, I don't know exactly when since the bug reports for these old versions have been deleted from the public site. Best way to deal with optimizer bugs is to give them a chance to fix them, keeping your compiler updated is pretty important. You've got 3 newer versions of the Express edition to choose from, that's two dog lives in compiler development. Three with C++11 around :)
Problem
The very simple code (under MS Visual C++ 2008 Express): ``` #include <iostream> using namespace std; int main() { for (int a=1; 2*a<=7; a++) cout << a << endl; return 0; } ``` Debug mode gives me correct result: ``` 1 2 3 ``` But Release mode gives me wrong result: ``` 1 2 ``` Well, I understand possible answers "use 2*a<8"; "why not a<=3", "a<4". I don't want to change the code, 'cause it is correct code (working well in Debug mode, all variables are initialized well, etc). - Have you the same bug with Visual C++ 2008 Express? - Is this bug present in younger versions (2010, 2012)? - How to avoid this bug? - Is there SP for fixing it? - Maybe to change some compile options (not default options)? Update: when I write ``` cout << a+1 << endl; ``` or ``` cout << 2*a << endl; ``` it works/compiles correct (3 rows of output). Note: I tried on different computers with VC++ 2008 Express. The same behavior.