473,320 Members | 1,865 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,320 software developers and data experts.

evaulation of boolean conditions...

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
9 2369
I meant &&, not || in my condition.

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

Jul 23 '05 #2
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
Thanks.

--John Ratliff
Jul 23 '05 #4
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
> 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
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


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


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

Similar topics

1
by: zilleeizad | last post by:
i am using a couple of bool flags to determine certain conditions. these flags are private members of a class. inside a method, i reset both the flags before i go into an if-else where if the...
7
by: Christopher Benson-Manica | last post by:
Given int g(); /* prototype */ int h(int); /* prototype */ void f( int x, int y ) /* x is either 0 or 1 */ { if( (/* other conditions */) && (/* other conditions */) && ( x == 1 || g()...
7
by: Daron | last post by:
In my SQL I have a column that returns a boolean value (-1 or 0), no problem. I need to be able to only see the records that return a 0 (False). I can sort and bring these to the top. When I...
43
by: Shehab Kamal | last post by:
Which approach is better: 1) Use a method that returns true/false (the method will log the exception that lead to false using log4net for example) OR 2) Use a method that returns void and throws...
1
by: kaloshi | last post by:
Hi I just began taking this class in C++, and I have an assignmetn to represent conditions as boolean expressions. For example: condition: payment must be at least 250.00 dollars solution: min...
2
by: Igor | last post by:
1. Are stored procedures WITH ENCRYPTION slower than the ones without encryption? 2. Should i put most restrictive conditions first or last in WHERE? In which order does MSSQL execute...
33
by: Stef Mientki | last post by:
hello, I discovered that boolean evaluation in Python is done "fast" (as soon as the condition is ok, the rest of the expression is ignored). Is this standard behavior or is there a compiler...
16
by: cmdolcet69 | last post by:
I have the below if statement, that should catch if any of the conditions are met.....however for some reasons if my boolDSIFlushGapReading = true and MuxClass.DSIValues.count =1 and my...
12
by: =?ISO-8859-1?Q?Ren=E9?= | last post by:
Hi, is there a rule of thumb what is better/faster/more performant in SQL Server 2005? a) SELECT * FROM A INNER JOIN B ON B.ID = A.ID AND B.Cond1 = 1 AND B.Cond2 = 2 b) SELECT * FROM A INNER...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.