Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

FIX: Inaccurate value is assigned to a variable by an assignment statement in a Visual C++ application that is compiled with the "/O2" optimization option in Visual Studio 2010


View products that this article applies to.

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. 

↑ Back to the top


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.

↑ Back to the top


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:Note 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 nameFile versionFile sizeDateTimePlatform
C2.dll16.0.40219.3812,608,38428-Jan-201200:06x86
C2.dll16.0.40219.3812,494,72001-Feb-201200:40x86
C2.dll16.0.40219.3812,593,53601-Feb-201200:40x86
C2.dll16.0.40219.3812,962,68801-Feb-201200:40x64
C2.dll16.0.40219.3817,359,74401-Feb-201200:40IA-64

↑ Back to the top


Status

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

↑ Back to the top


Workaround

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

↑ Back to the top


More Information

For more information about Visual C++ compiler options /O1 and /O2, visit the following MSDN website:
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 6
However, the expected result is as follows:
1 3 6 10

↑ Back to the top


Keywords: kb, kbqfe, kbfix, kbhotfixdev, kbexpertiseadvanced, kbhotfixserver, kbsurveynew

↑ Back to the top

Article Info
Article ID : 2667938
Revision : 1
Created on : 1/7/2017
Published on : 3/15/2012
Exists online : False
Views : 291