Search

C++ compiler BUG with unsigned types in loops by lkj23

Closed
as Deferred Help for as Deferred

1
0
Sign in
to vote
Type: Bug
ID: 777904
Opened: 1/30/2013 9:44:13 AM
Access Restriction: Public
0
Workaround(s)
0
User(s) can reproduce this bug
C++ compiler in Visual Studio 2010 probably contains BUGs.
It produces incorrect code, if we use "unsigned" type in "for" loops and use -O2 optimization.
Details (expand)

Visual Studio/Team Foundation Server/.NET Framework Tooling Version

Visual Studio 2012

Steps to reproduce

The code for Visual Studio 2010 C++ compiler.
If compiled with -O1 optimization, it works OK.
If compiled with -O2 optimization, the result is incorrect.

#include <stdio.h>

unsigned size = ((unsigned)1 << 31);
void main()
{
unsigned size2 = size;
unsigned sum = 0;
for (unsigned i = 0; i < size2; i++)
    sum += (i & 1);
printf("sum = %x", sum);
}

Product Language

English

Operating System

Any

Operating System Language

English

Actual results

sum = 0

Expected results

sum = 40000000
File Attachments
0 attachments
Sign in to post a comment.
Posted by Microsoft on 2/22/2013 at 1:51 PM
Thanks for reporting this bug. We have verified the bug and will release a fix in the future.

If you need a workaround for the VS 2010 compiler, you can turn off optimizations for the specific function using: #pragma optimize("", off) ... your_func() { ... } #pragma optimize("", on). If you need a hotfix for VS 2010, please visit the support site: http://support.microsoft.com/common/international.aspx?RDPATH=dm;en-us;selectassist&target=assistance

Thanks,
Rui Zhang
Microsoft Visual C++
Posted by Microsoft on 1/30/2013 at 6:57 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 1/30/2013 at 9:54 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.