Jump to content
  • Advertisement
Sign in to follow this  
steve coward

Operation precedence within function call (debug vs. release)

This topic is 3524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am seeing a difference in order of operation in evaluating a parameter to my StackPush() function call:
StackPush ( -StackPopNumber ().number + StackPopNumber ().number);

In debug mode it appears that the first Pop occurs before the second. In release mode it appears that the second Pop occurs before the first. This is C++ with Visual Express 2005. Any ideas? Details - Release, Debug ============== stack top is 7.0 next entry is 175.0
double top = StackPopNumber().number;
double next = StackPopNumber ().number;
StackPush ( next - top );

stack top now is 168 Debug ======= stack top is 7.0 next entry is 175.0
						
StackPush ( -StackPopNumber ().number + StackPopNumber ().number);

stack top now is 168 Release ======= stack top is 7.0 next entry is 175.0
						
StackPush ( -StackPopNumber ().number + StackPopNumber ().number);

stack top now is -168 ?????????
void	StackPush(const double p)
{
	Token t;
	t.type = NUMBER;
	t.number = p;
	TokenStack.push (t);
}

Token	StackTop()
{
	return TokenStack.top ();
}

Token	StackPopNumber()
{
	Token ret;
			
	while (StackSize() > 0)
	{
		ret = TokenStack.top ();
		TokenStack.pop ();
		if (ret.type == NUMBER)
		{
			return ret;
		}
	}
	ret.type = NUMBER;
	ret.number = 0.0;
	return ret;
}

struct Token
{
	TokenType		type;

	union
	{
		double		number;
		char		identifier[128];
	};

	Token	()
	{
	}

	Token	(const TokenType t)
	{
		type = t;
	}

	Token	(const TokenType t, const double n)
	{
		type = t;
		number = n;
	}

	Token	(const TokenType t, const char * i, int len)
	{
		type = t;
		strncpy (identifier, i, len);
		identifier[len] = '\0';
	}
};

Share this post


Link to post
Share on other sites
Advertisement
Sequence points.

Quote:
From the article above
Consider two functions f() and g(). In C and C++, the + operator is not a sequence point, and therefore in the expression f()+g() it is possible that either f() or g() will be executed first.


In other words, it is undefined which one is executed first, so it is entirely possible that a debug and release build will change the order, as indeed is pretty much any other possibility.

Share this post


Link to post
Share on other sites
And for whatever it may be worth, I tried something similar in VS2008 and it seems to be consistent, thought perhaps it depends on a specific situation or optimization settings.

Share this post


Link to post
Share on other sites
Quote:
Original post by EasilyConfused
In other words, it is undefined which one is executed first, so it is entirely possible that a debug and release build will change the order, as indeed is pretty much any other possibility.
And just to reiterate that in stronger language, you should never, under any circumstances, rely on the order of evaluation of function arguments.

Share this post


Link to post
Share on other sites
Thanks for the quick replies.

I verified that the same behavior occurs in a simple assignment
such as a = -f(x) + g(x), where both f(x) and g(x) read and pop
the same stack of arguments.

Share this post


Link to post
Share on other sites
Again, in your assignment there's no sequence point between the evaluation of f() and g(), so the order in which they are called is unspecified.

Share this post


Link to post
Share on other sites
Also, note that this has nothing to do with operator precedence (despite what your title says). Operator precedence is used to determine where to place parentheses in an expression that involves two or more operations So, for instance, if you write a + b * c, operator precedence rules will group this as a + (b * c). However, this still doesn't determine which of 'a', 'b' or 'c' will be evaluated first.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!