Incorrect code generated when adding array of 4 char to array of short in a loop - by David Hough

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 672163 Comments
Status Closed Workarounds
Type Bug Repros 1
Opened 6/1/2011 6:28:46 AM
Access Restriction Public


The compiled code generated for the following C++ example produces different output between the Debug and Release configurations. It should print out "0 1 2 3", but the Release build actually prints out "0 0 2 3". 

Stepping through in the debugger, it appears the snippet

for(int i = 0; i < 4; i++)
	accumulate[i] += new_vals[i];

is where it goes wrong, as it appears to get compiled too be the equivalent of

accumulate[0] += new_vals[0]
accumulate[1] += new_vals[0]
accumulate[2] += new_vals[2]
accumulate[3] += new_vals[3]

Note, this behaviour seems to be affected by what the "Whole Program Optimization" option is set too. The incorrect behaviour of the Release build is seen when this is set to "No Whole Program Optimization", but the behaviour is correct if this is set to "Use Link Time Code Generation"

Example program:

#include <cstdio>

// expected output 
//  0 1 2 3
// Release build with "Whole Program Optimization" disabled outputs
//  0 0 2 3

class foo
		for(int i = 0; i < 4; i++)
			accumulate[i] = 0;

	short accumulate[4];

	void load_new_vals(char new_vals[4]);
	void test();

void foo::load_new_vals(char new_vals[4])
	for(int i = 0; i < 4; i++)
		new_vals[i] = i;

void foo::test()
	char new_vals[4];

	for(int i = 0; i < 4; i++)
		accumulate[i] += new_vals[i];

	printf("%d %d %d %d\n", accumulate[0], accumulate[1], accumulate[2], accumulate[3]);

int main(int argc, char** argv)
	foo bar;


	return 0;
Sign in to post a comment.
Posted by Microsoft on 6/3/2011 at 10:32 AM
Thanks for reporting this issue. The incorrect code generation in this case is the result of an incorrect transformation of "MOVSX AX, AH" into a CBW instruction (CBW is the equivalent of MOVSX AX, AL). This problem will be fixed in a future release of the Visual C++ compiler.
In the meantime you can work around this problem by avoiding 16-bit arithmetic or disabling optimizations via #pragma optimize() in the function with incorrect code generation (foo::test in the provided example).

ian Bearman
Visual C++ Code Generation and Optimization
Posted by EricLeong [Feedback Moderator] on 6/1/2011 at 6:58 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 MS-Moderator01 on 6/1/2011 at 6:49 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(