Symptoms

Consider the following scenario:

  • You use Microsoft Visual Studio 2010 Service Pack 1 (SP1) to develop a Visual C++ application.

  • The source code of the application contains an assignment statement within a loop with a cross iteration loop dependency.

  • You use Visual Studio 2010 to compile the application, targeting the x86, x64, or Itanium platform.

  • You use the /O2 optimization option when you compile the application.

In this scenario, an inaccurate value is assigned to the variable that is used inside the loop. 

Cause

This issue occurs because a cross iteration loop dependency is not detected by the Visual C++ 2010 compiler when you use the variable in the loop. Therefore, incorrect machine code is generated.

Resolution

Hotfix information

A supported hotfix is now available from Microsoft. However, it is intended to correct only the problem that is described in this article. Apply it only to systems that are experiencing this specific problem. This hotfix may receive additional testing. Therefore, if you are not severely affected by this problem, we recommend that you wait for the next software update that contains this hotfix.To resolve this problem immediately, contact Microsoft Customer Support Services to obtain the hotfix. For a complete list of Microsoft Customer Support Services telephone numbers and information about support costs, visit the following Microsoft website:

http://support.microsoft.com/contactus/?ws=supportNote In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

Prerequisites

To apply this hotfix, you must have Visual Studio 2010 SP1 installed.

Restart requirement

You must restart the computer after you install this hotfix if any instance of Visual Studio 2010 is running.

Hotfix replacement information

This hotfix does not replace any other hotfix.

File Information

The English version of this hotfix has the file attributes (or later file attributes) that are listed in the following table. The dates and times for these files are listed in Coordinated Universal Time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time item in Control Panel.

For all supported versions of Visual Studio 2010

File name

File version

File size

Date

Time

Platform

C2.dll

16.0.40219.381

2,608,384

28-Jan-2012

00:06

x86

C2.dll

16.0.40219.381

2,494,720

01-Feb-2012

00:40

x86

C2.dll

16.0.40219.381

2,593,536

01-Feb-2012

00:40

x86

C2.dll

16.0.40219.381

2,962,688

01-Feb-2012

00:40

x64

C2.dll

16.0.40219.381

7,359,744

01-Feb-2012

00:40

IA-64

Status

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

Workaround

To work around this issue, use code that resembles the commented code that is described in the "More information" section.

More Information

For more information about Visual C++ compiler options /O1 and /O2, visit the following MSDN website:

General information about Visual C++ compiler options /O1 and /O2

Steps to reproduce this problem

To reproduce this issue, use the following code to generate a Visual C++ application:

#include <stdio.h>#include <tchar.h>#include <memory.h>#include <malloc.h>typedef struct Run {            int value;} Run;int _tmain(int argc, _TCHAR* argv[]){            TCHAR* szCount = _T("4");            int count = _ttoi(szCount);            size_t size = count * sizeof(Run);            Run* pRun = (Run*) malloc(size);            memset(pRun, 0, size);            size = count * sizeof(unsigned char);            unsigned char* pLevels = (unsigned char*) malloc( size);            memset(pLevels, 0, size);            for(int i = 0; i < count; ++i)            {                        pRun[i].value = 1 + i;            }            int symbol = 0;            int i;            for(i=0; i<count; ++i)             {                        symbol=pRun[i].value+=symbol;                       //Work around method                       //pRun[i].value += symbol;                                            //symbol = pRun[i].value;}            for(int i = 0; i < count; ++i)            {                        printf("%d ", pRun[i].value);            }            printf("\r\n");            free(pRun);            return 0;}

You compile the application with the /O2 optimization option, and then you receive the following incorrect result:1 2 4 6However, the expected result is as follows:1 3 6 10

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.