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

evaulation of boolean conditions...

P: n/a
Does C++ employ lazy evaluation of boolean conditions, e.g. can I do the
following safely

if ((obj != NULL) || (obj->someMethod())) {
// blah blah
}

Or will I get a segfault if the obj ptr is NULL? This is fine in Java, but I
wasn't sure about C++.

Thanks,

--John Ratliff


Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
I meant &&, not || in my condition.

if ((obj != NULL) && (obj->someMethod())) {
// blah blah
}

Jul 23 '05 #2

P: n/a
Yes, C++ uses short circuit OR, i.e If the first expression turns out
to be false , it wont evaluate further expressions.
- Nirmalya

John Ratliff wrote:
I meant &&, not || in my condition.

if ((obj != NULL) && (obj->someMethod())) {
// blah blah
}


Jul 23 '05 #3

P: n/a
Thanks.

--John Ratliff
Jul 23 '05 #4

P: n/a
In C++
(a && b)

The first operand ( a) is completely evaluated and all side effects are
completed before continuing evaluation of the logical AND expression.

The second operand (b) is evaluated only if the first operand
evaluates to true (nonzero). This evaluation eliminates needless
evaluation of the second operand when the logical AND expression is
false. You can use this short-circuit evaluation to prevent
null-pointer dereferencing, as shown in the following example:
..
if ((obj != NULL) && (obj->someMethod()))
If obj is null (0), the right side of the expression is never
evaluated.

Jul 23 '05 #5

P: n/a
> Does C++ employ lazy evaluation of boolean conditions, e.g. can I do the
following safely if ((obj != NULL) || (obj->someMethod())) {
// blah blah
}


Yes. If obj is NULL, the second part of the conditional will not be
evaluated.

Regards,
Srini

Jul 23 '05 #6

P: n/a
I think this is OK.
Provided you have instantiated your class in this way using new
operator

Obj *obj = new Obj();

Jul 23 '05 #7

P: n/a


upashu2 wrote:
In C++
(a && b)

The first operand ( a) is completely evaluated and all side effects are
completed before continuing evaluation of the logical AND expression.

The second operand (b) is evaluated only if the first operand
evaluates to true (nonzero). This evaluation eliminates needless
evaluation of the second operand when the logical AND expression is
false. You can use this short-circuit evaluation to prevent
null-pointer dereferencing, as shown in the following example:
.
if ((obj != NULL) && (obj->someMethod()))
If obj is null (0), the right side of the expression is never
evaluated.


But there's a catch.

The correct answer is that righthand side of the && expression in the
sample code provided ( (obj->someMethod()))) may or may not be
evaluated even when objc != NULL is false. There is not enough in the
sample provided to answer for certain.

Only the built-in && operator is guaranteed not to evaluate the
righthand side of the expression if the lefthand side is false. A
user-defined && operator would always force an evaluation of the
righthand side of its expression since it is passed as a parameter to
the operator&& routine.

Since the code above omits obj's declaration, the && operator (and the
!= operator as well) could have been overloaed in the case that obj is
declared as some kind of a smart pointer type.

Greg

Jul 23 '05 #8

P: n/a
Geo


Nirmalya Ghosh Chowdhury wrote:
Yes, C++ uses short circuit OR, i.e If the first expression turns out
to be false , it wont evaluate further expressions.
- Nirmalya

John Ratliff wrote:
I meant &&, not || in my condition.

if ((obj != NULL) && (obj->someMethod())) {
// blah blah
}


I think you meant AND, OR will will stop at the first 'true' expression.

Jul 23 '05 #9

P: n/a


am*********@gmail.com schreef:
I think this is OK.
Provided you have instantiated your class in this way using new
operator

Obj *obj = new Obj();


No, it doesn't matter how it was created. The following is also OK

void foo( Obj* obj )
{
if( Obj && Obj->SomeBool )
// ....
}
void bar( )
{
Obj o;
foo( &o );
}

BTW, you know that (new Obj) never returns 0? If it fails it throws
std::bad_alloc so you don't have to test it.

HTH
Michiel Salters

Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.