visual studio 2005 2008 2010 bug in c++ optimizer for win 32 platform - by hamid alaei

Status : 

  By Design<br /><br />
		The product team believes this item works according to its intended design.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


1
1
Sign in
to vote
ID 519445 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 12/9/2009 11:14:25 PM
Access Restriction Public

Description

Hello!
I found a very bad bug in Visual Studio 2005 and 2008 and 2010. This bug exists in C++ optimizer.  For seeing the bug execute this code with and without optimizing speed (or in debug and release mode in win 32 platform with default project properties). The outputs differ!
#include <stdio.h>
int EP(int& n);
int TP(int& n);
int F(int& n);
int main()
{
	int n=2;
	int e=F(n)*TP(n)+EP(n);
	printf("%d\n",e);
	return 0;
}
int EP(int& n)
{
	if(n>0)
	{
		n--;
		return F(n)*TP(n)+EP(n);
	}
	return 0;
}
int TP(int& n)
{
	if(n>0)
	{
		n--;
		return F(n)*TP(n);
	}
	return 1;
}
int F(int& n)
{
	if(n>0)
	{
		n--;
		int e= F(n)*TP(n)+EP(n);
		n--;
		return e;
	}
	return 2;
}

Can you debug this as fast as possible and give me the solution? Please contact me anyway, because it is vital for me.
Sign in to post a comment.
Posted by hamid alaei on 12/14/2009 at 10:23 AM
thank for your illogicality.
Posted by Microsoft on 12/14/2009 at 8:55 AM
Hi: this is By-Design. In C++ (and C) in most cases there is no guarantee about the order in which the operands of an expression are evaluated. The Debug build does very little optimization and therefore the expressions are evaluated in the "natural" order - in a Release build (especially with inlining enbalbe) I wouldn't even want to attempt to predict the order of evaluation of some of the expressions in your program. In short if one sub-expression depends on a side-effect from another sub-expression then all bets are off. Take a look at "order-of-evaluation" and/or "sequence-points" in a good C++ book.

Jonathan Caves
Visual C++ Compiler Team
Posted by Microsoft on 12/14/2009 at 1:31 AM
Thanks for your feedback. We were able to reproduce the issue you are seeing. We are routing 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 Microsoft on 12/11/2009 at 3:15 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)