Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual C++ 2008 bug?

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.

like image 800
Oleg567 Avatar asked Nov 19 '13 09:11

Oleg567


People also ask

Can I delete Microsoft Visual C++ 2008?

Go to Programs and Features in Control Panel and identify the relevant Microsoft Visual C++ Redistributable. Right Click this and select Uninstall.

Is Microsoft Visual C++ 2008 needed?

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.

What happens if I uninstall Microsoft Visual C++ 2008 redistributable?

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 . . .

Can I safely remove Microsoft Visual C++?

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.


1 Answers

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 :)

like image 141
Hans Passant Avatar answered Nov 14 '22 18:11

Hans Passant