Invalid C code result with optimization - by SolidSnake2011

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.


1
0
Sign in
to vote
ID 676931 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 6/25/2011 1:18:00 AM
Access Restriction Public
Moderator Decision Sent to Engineering Team for consideration

Description

The following code gives invalid result with optimization (Release configuration).

#include <stdio.h>
#include <stdlib.h>

typedef char T; // char or short
#define N 4 // any from 4 to 10

void
int2Buf (T * buf, int value, int nBits)
{
	int i;
	for (i = 0 ; i < nBits; i++) {
		buf[i] = value & 1;
		value >>= 1;
	}
}

void
printBuf (T * buf, int nBits)
{
	int i;
	printf ("bits: ");
	for (i = 0; i < nBits; i++) {
		printf ("%d", (int)buf[i]);
	}
	printf ("\n");
}

int
main (void)
{
	T buf[32];
	int2Buf (buf, 0x55555555, N);
	printBuf (buf, N);
	int2Buf (buf, abs(0x55555555), N);
	printBuf (buf, N);
	return 0;
}
Sign in to post a comment.
Posted by Microsoft on 6/28/2011 at 1:01 PM
Thanks for reporting this issue. This will be address in the next release of Visual Studio. You can work around optimizations such as this by disabling optimizations around the function that is misbehaving. Documentation on doing this can be found on MSDN here:
http://msdn.microsoft.com/en-us/library/chh3fb0k(v=VS.100).aspx

thanks,
ian Bearman
VC++ Code Generation and Optimization Team
Posted by MS-Moderator10 [Feedback Moderator] on 6/26/2011 at 7:29 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 MS-Moderator01 on 6/25/2011 at 1: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(http://support.microsoft.com)