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).
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.
Go to Programs and Features in Control Panel and identify the relevant Microsoft Visual C++ Redistributable. Right Click this and select Uninstall.
There may be different versions of Microsoft Visual C++ Redistributable files. They would be for 2005, 2008 and 2010. Many times software requires these files and automatically install the required files. I do not suggest you delete them.
No, it is not safe to delete any of those, they are installed by the applications that you installed onto your PC, those applications target different versions of the C++ redistributable and will cease to function properly if you delete any of those . . .
You can uninstall the Visual C++ Runtime Libraries automatically with the help of a free tool available on the Major Geeks website. Simply download and run the Visual C++ Redistributable Runtimes AIO Repack.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With