Sign in to follow this  

Question about if statements

This topic is 3292 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

This is in c++ For example given this statement
int *x = NULL;

if( x[0] == 1 && x != NULL )
{
}
This is probably bad obviously but how would c++ handle it like in what order would c++ evaluate each condition and how would that work exactly... Sorry if it sounds confusing but I really don't know how to explain it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nisss
This is in c++

For example given this statement


int *x = NULL;

if( x[0] == 1 && x != NULL )
{
}


This is probably bad obviously but how would c++ handle it like in what order would c++ evaluate each condition and how would that work exactly... Sorry if it sounds confusing but I really don't know how to explain it.
C++ always evaluates operators && and || in left-to right order. It's actually perfectly acceptable to do something similar to what you have, but the other way around. You may well see:
if(x != NULL && x[0] == 1)

Function parameters are another issue however. The following is undefined:
int x=0;
printf("%d, %d", x++, x++);

EDIT: It's called "short circuit evaluation", if the left side of the && fails, there's no way the if statement can succeed, so the right side won't be evaluated. So x will never be dereferenced.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
Function parameters are another issue however. The following is undefined:
int x=0;
printf("%d, %d", x++, x++);


I thought that if you type ++x it will pass 1 and x++ would pass 0. That's the way I learned.

Share this post


Link to post
Share on other sites
Quote:
Original post by mystb
Quote:
Original post by Evil Steve
Function parameters are another issue however. The following is undefined:
int x=0;
printf("%d, %d", x++, x++);


I thought that if you type ++x it will pass 1 and x++ would pass 0. That's the way I learned.
For a single statement, that's fine. The following does as you thought:

int x = 0;
int a = ++x; // x is pre-incremented to 1, then assigned to a. a=1
int b = x++; // x is assigned to b, then post-incremented to 2. b=1



However, the order the arguments to a function are evaluated in is undefined (I believe it usually depends on the calling convention, but there's no guarantee of that). The printf() I posted above could print out "0, 0", "0, 1", "1, 0", or "1, 1", and so on depending on how the compiler decides to do things.

The compiler could evaluate all arguments before calling the function, that'd give "2, 2", or it could pass them to the function as it evaluates them left-to-right and give "0, 1", or it could do all sorts of other things.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
The compiler could evaluate all arguments before calling the function

No, the compiler ALWAYS does that. A function call is a sequence point. That is, the side effects of both x++ expressions are guaranteed to have taken effect before the function is called. The interesting part is that there are no sequence points between the evaluations of the arguments. So you do not know if x has already been incremented when you evaluate the second x++.

Share this post


Link to post
Share on other sites

This topic is 3292 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this