Connecting Tech Pros Worldwide Help | Site Map

Will the second be evaluated if the first is NULL?

William Payne
Guest
 
Posts: n/a
#1: Jul 22 '05
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


Karl Heinz Buchegger
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Will the second be evaluated if the first is NULL?


William Payne wrote:[color=blue]
>
> 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?
>[/color]

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
kbuchegg@gascad.at
Karl Heinz Buchegger
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Will the second be evaluated if the first is NULL?


William Payne wrote:[color=blue]
>
> 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?
>[/color]

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
kbuchegg@gascad.at
Julie
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Will the second be evaluated if the first is NULL?


William Payne wrote:[color=blue]
>
> 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?[/color]

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

The behavior is called 'short circuit', and evaluated left to right.
Julie
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Will the second be evaluated if the first is NULL?


William Payne wrote:[color=blue]
>
> 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?[/color]

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

The behavior is called 'short circuit', and evaluated left to right.
William Payne
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Will the second be evaluated if the first is NULL?



"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:4076B7DC.4BEC7B4D@gascad.at...[color=blue]
> William Payne wrote:[color=green]
> >
> > 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[/color][/color]
if[color=blue][color=green]
> > fooptr is NULL? Can I make sure it is evaluated from left to right or[/color][/color]
should[color=blue][color=green]
> > I have nested if-statements instead?
> >[/color]
>
> 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
> kbuchegg@gascad.at[/color]

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


William Payne
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Will the second be evaluated if the first is NULL?



"Karl Heinz Buchegger" <kbuchegg@gascad.at> wrote in message
news:4076B7DC.4BEC7B4D@gascad.at...[color=blue]
> William Payne wrote:[color=green]
> >
> > 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[/color][/color]
if[color=blue][color=green]
> > fooptr is NULL? Can I make sure it is evaluated from left to right or[/color][/color]
should[color=blue][color=green]
> > I have nested if-statements instead?
> >[/color]
>
> 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
> kbuchegg@gascad.at[/color]

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


Closed Thread