473,394 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Will the second be evaluated if the first is NULL?

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
6 1608
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
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
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
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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mike Chirico | last post by:
Hopefully this will help someone... Helpful Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Last Updated: Fri Apr 16 11:47:34 EDT 2004 The latest version of this...
6
by: William Payne | last post by:
Consider the following code: struct foo { char* bar; }; // fooptr is of type foo* if(fooptr != NULL && fooptr->bar != NULL) {
18
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
3
by: tconkling | last post by:
I have an if statement that looks like this: if(foo(&x) && x > y) ... where the value of x is modified by foo, and the comparison between x and y only makes sense after x has been modified by...
1
by: Steph | last post by:
I am trying to save a simple integer value in ViewState. I set the value in the Page_Load() event and retrieve it in the function Page_Unload() event. The value seems to be set correctly, but...
1
by: Sky | last post by:
Yesterday I was told that GetType(string) should not just be with a Type, but be Type, AssemblyName. Fair enough, get the reason. (Finally!). As long as it doesn't cause tech support problems...
4
by: Divick | last post by:
Hi, does sizeof operator evaluate the size at compile time or run time ? In other words is sizeof(SomeType) evaluated at compile time or at runtime? Please provide the reasoning involved as well....
2
by: RobAMacAF | last post by:
I have a program I am trying to write and I have it working the first time. The user clicks the OPEN button to open a txt file which the program loads into it's arrays. Everything works great until...
15
by: puzzlecracker | last post by:
I see that a lot of former in the code, and wonder if there is a technical reason for that
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.