By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,537 Members | 929 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,537 IT Pros & Developers. It's quick & easy.

Will the second be evaluated if the first is NULL?

P: n/a
Consider the following code:

struct foo
{
char* bar;
};

// fooptr is of type foo*
if(fooptr != NULL && fooptr->bar != NULL)
{
cout << fooptr->bar << endl;
}

Is the if-statement safe, meaning it won't try to evaluate fooptr->bar if
fooptr is NULL? Can I make sure it is evaluated from left to right or should
I have nested if-statements instead?

Thanks for any replies

/ William Payne
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
William Payne wrote:

Consider the following code:

struct foo
{
char* bar;
};

// fooptr is of type foo*
if(fooptr != NULL && fooptr->bar != NULL)
{
cout << fooptr->bar << endl;
}

Is the if-statement safe, meaning it won't try to evaluate fooptr->bar if
fooptr is NULL? Can I make sure it is evaluated from left to right or should
I have nested if-statements instead?


In x && y, it is guaranteed that y is not evaluated if x is already false.
Same for x || y : if x is already true, y is not evaluated at all.

Which books are you using, that do not mention 'shortcut evaluation' ?
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a
William Payne wrote:

Consider the following code:

struct foo
{
char* bar;
};

// fooptr is of type foo*
if(fooptr != NULL && fooptr->bar != NULL)
{
cout << fooptr->bar << endl;
}

Is the if-statement safe, meaning it won't try to evaluate fooptr->bar if
fooptr is NULL? Can I make sure it is evaluated from left to right or should
I have nested if-statements instead?


In x && y, it is guaranteed that y is not evaluated if x is already false.
Same for x || y : if x is already true, y is not evaluated at all.

Which books are you using, that do not mention 'shortcut evaluation' ?
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3

P: n/a
William Payne wrote:

Consider the following code:

struct foo
{
char* bar;
};

// fooptr is of type foo*
if(fooptr != NULL && fooptr->bar != NULL)
{
cout << fooptr->bar << endl;
}

Is the if-statement safe, meaning it won't try to evaluate fooptr->bar if
fooptr is NULL? Can I make sure it is evaluated from left to right or should
I have nested if-statements instead?


You're golden -- yes, the statement is safe.

The behavior is called 'short circuit', and evaluated left to right.
Jul 22 '05 #4

P: n/a
William Payne wrote:

Consider the following code:

struct foo
{
char* bar;
};

// fooptr is of type foo*
if(fooptr != NULL && fooptr->bar != NULL)
{
cout << fooptr->bar << endl;
}

Is the if-statement safe, meaning it won't try to evaluate fooptr->bar if
fooptr is NULL? Can I make sure it is evaluated from left to right or should
I have nested if-statements instead?


You're golden -- yes, the statement is safe.

The behavior is called 'short circuit', and evaluated left to right.
Jul 22 '05 #5

P: n/a

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
William Payne wrote:

Consider the following code:

struct foo
{
char* bar;
};

// fooptr is of type foo*
if(fooptr != NULL && fooptr->bar != NULL)
{
cout << fooptr->bar << endl;
}

Is the if-statement safe, meaning it won't try to evaluate fooptr->bar if fooptr is NULL? Can I make sure it is evaluated from left to right or should I have nested if-statements instead?


In x && y, it is guaranteed that y is not evaluated if x is already false.
Same for x || y : if x is already true, y is not evaluated at all.

Which books are you using, that do not mention 'shortcut evaluation' ?
--
Karl Heinz Buchegger
kb******@gascad.at


Thanks for the quick reply, Karl. No need for a nested if-statement then,
just as I thought. And to answer your question: I don't have access to a C++
text except Josuttis Standard Library Book at the moment. =/

/ William Payne
Jul 22 '05 #6

P: n/a

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...
William Payne wrote:

Consider the following code:

struct foo
{
char* bar;
};

// fooptr is of type foo*
if(fooptr != NULL && fooptr->bar != NULL)
{
cout << fooptr->bar << endl;
}

Is the if-statement safe, meaning it won't try to evaluate fooptr->bar if fooptr is NULL? Can I make sure it is evaluated from left to right or should I have nested if-statements instead?


In x && y, it is guaranteed that y is not evaluated if x is already false.
Same for x || y : if x is already true, y is not evaluated at all.

Which books are you using, that do not mention 'shortcut evaluation' ?
--
Karl Heinz Buchegger
kb******@gascad.at


Thanks for the quick reply, Karl. No need for a nested if-statement then,
just as I thought. And to answer your question: I don't have access to a C++
text except Josuttis Standard Library Book at the moment. =/

/ William Payne
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.