optimization bug - by anthonyjoquinto

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 650228 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 3/8/2011 12:41:41 PM
Access Restriction Public


The programming language is C++ and the bug only happens when in Release, in Debug everything is fine.

When trying to do a sum of the elements in a char array in a loop or one after the other, the result is not the expected one. This problem happens when the sum is made in a char with a succession of "+" and the target char is 0 at the beginning.

#include <iostream>

int main()
	unsigned char byContent[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
	unsigned char *data = new unsigned char[2048];

	memcpy(data, byContent, 16);

	unsigned int *actualData = (unsigned int*) data;
	unsigned int uiCheckSum = 0;
	unsigned int cnt = 0;

	//the 4 is 16/4, the 16 is the size given to memcpy
	for (cnt=0; cnt<4; cnt++, actualData++)
		uiCheckSum += *actualData;

	unsigned char checkSum = 0;
	unsigned char *totalCheckSum = (unsigned char*)&uiCheckSum;
	/*for (cnt=0; cnt<4; cnt++) 
		//printf("losing time");
		checkSum = (unsigned char)(checkSum + totalCheckSum[cnt]);

	//printf("totalCheckSum[0] = %i\n", totalCheckSum[0]);
	//printf("totalCheckSum[1] = %i\n", totalCheckSum[1]);
	//printf("totalCheckSum[2] = %i\n", totalCheckSum[2]);
	//printf("totalCheckSum[3] = %i\n", totalCheckSum[3]);

	checkSum = (unsigned char)(checkSum + totalCheckSum[0]);
	checkSum = (unsigned char)(checkSum + totalCheckSum[1]);
	printf("checkSum = %i and should be equal to 52 (24 + 28)\n", checkSum);
	checkSum = (unsigned char)(checkSum + totalCheckSum[2]);
	checkSum = (unsigned char)(checkSum + totalCheckSum[3]);

	printf("checkSum = %i and should be equal to 120", checkSum);

	while(1) {}

The first summation seems to modify the value of the next char in the array. If checkSum is not 0 at the beginning, everything is fine and if the printf() are not commented, it works too because there is no optimization. 
Sign in to post a comment.
Posted by Microsoft on 3/9/2011 at 2:12 PM
Thanks for reporting this. The problem will be fixed in the next major release of Visual Studio. In the meantime you can work around this by using #pragma optimize to disable optimization around the function exhibiting incorrect behavior. See http://msdn.microsoft.com/en-us/library/chh3fb0k(v=VS.100).aspx for details on how to use this pragma.

VC++ Code Generation and Optimization Team
Posted by Microsoft on 3/8/2011 at 6:14 PM
Thank you for submitting feedback on Visual Studio 2010 and .NET Framework. Your issue has been routed to the appropriate VS development team for review. We will contact you if we require any additional information.
Posted by Microsoft on 3/8/2011 at 1:13 PM
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)