473,796 Members | 2,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1628
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******@gasca d.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******@gasca d.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
1498
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 document can be found at: http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download Format is better on the above link. I'm looking for suggestions and
6
341
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
2954
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
53
8225
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 all-bits-zero, and does not therefore guarantee useful null pointer values (see section 5 of this list) or floating-point zero values.
3
1564
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 foo (and, of course, if foo returns true). Am I guaranteed (assuming my compiler generates correct code) that x > y is evaluated after foo(&x) returns?
1
2418
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 upon the next Page_Load(), I get the following error when I try to retrieve the value from ViewState: "function 'ViewState.get_Item' evaluated and returned null". The page does have a form on it with the runat="server" attribute set. From the...
1
2158
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 down the line... What happens when my code is run on a station that only has framework 3.0 or 4.0, and this assembly, with version number defined for 2.0.0.0 , isn't available. ...breaks? Second question: Does an assembly's PublicKeyToken change...
4
6062
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. TIA, Divick
2
1031
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 they try to open another one. When they do they get a nullreferenceerror private void GetQuestionList(string InputList) { StreamReader fs = File.OpenText(InputList); string input = null; ...
15
2418
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
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10461
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9057
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.