473,396 Members | 2,013 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,396 software developers and data experts.

can you do if((myptr) && (myptr->IsGreen()) without blowing up?

If I need to check if a pointer is valid I usually do a if(ptr) check
first.

But I have seen code such as this:

if((myptr) && (myptr->IsGreen())

Is this valid?

Is it because evaluation is from left to right so that the if(ptr) bit
is evaluated first. Then only if pointer is valid is right function
called?
Jun 27 '08 #1
12 1299
On 20 Jun., 18:20, Angus <anguscom...@gmail.comwrote:
If I need to check if a pointer is valid I usually do a if(ptr) check
first.

But I have seen code such as this:

if((myptr) && (myptr->IsGreen())

Is this valid?
Yes, this is guaranteed to work.
>
Is it because evaluation is from left to right so that the if(ptr) bit
is evaluated first. *Then only if pointer is valid is right function
called?
This is what is required in the standard, and all compilers adhere to
this requirement that dates back to C.

I would avoid the pointers and clarify by writing: if (myptr != 0 &&
myptr->IsGreen()), but this is a personal choice.

/Peter
Jun 27 '08 #2
Angus wrote:
If I need to check if a pointer is valid I usually do a if(ptr) check
first.

But I have seen code such as this:

if((myptr) && (myptr->IsGreen())

Is this valid?
Yes.
Is it because evaluation is from left to right so that the if(ptr) bit
is evaluated first. Then only if pointer is valid is right function
called?
The formal reason is the guarantee in [5.14/1]:

The && operator groups left-to-right. The operands are both implicitly
converted to type bool (clause 4). The result is true if both operands are
true and false otherwise. Unlike &, && guarantees left-to-right
evaluation: the second operand is not evaluated if the first operand is
false.
Best

Kai-Uwe Bux
Jun 27 '08 #3
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:g3**********@aioe.org...
Angus wrote:
>If I need to check if a pointer is valid I usually do a if(ptr) check
first.

But I have seen code such as this:

if((myptr) && (myptr->IsGreen())

Is this valid?

Yes.
>Is it because evaluation is from left to right so that the if(ptr) bit
is evaluated first. Then only if pointer is valid is right function
called?

The formal reason is the guarantee in [5.14/1]:

The && operator groups left-to-right. The operands are both implicitly
converted to type bool (clause 4). The result is true if both operands
are
true and false otherwise. Unlike &, && guarantees left-to-right
evaluation: the second operand is not evaluated if the first operand is
false.
As a note, this is sometimes refered to as "short circuiting". It will also
work with ||
if ( cond1 || cond2 )

cond2 will only be evaluated if cond1 is false.
Jun 27 '08 #4
On 20 Jun, 18:04, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Kai-Uwe Bux" <jkherci...@gmx.netwrote in message

news:g3**********@aioe.org...
Angus wrote:
If I need to check if a pointer is valid I usually do a if(ptr) check
first.
But I have seen code such as this:
if((myptr) && (myptr->IsGreen())
Is this valid?
Yes.
Is it because evaluation is from left to right so that the if(ptr) bit
is evaluated first. *Then only if pointer is valid is right function
called?
The formal reason is the guarantee in [5.14/1]:
*The && operator groups left-to-right. The operands are both implicitly
*converted to type bool (clause 4). The result is true if both operands
are
*true and false otherwise. Unlike &, && guarantees left-to-right
*evaluation: the second operand is not evaluated if the first operandis
*false.

As a note, this is sometimes refered to as "short circuiting". *It willalso
work with ||
if ( cond1 || cond2 )

cond2 will only be evaluated if cond1 is false.
Note however that this is a special feature of the operators && and
||. If you do something like:

x = a() + b();

there is no guarantee which of a and b will be called first.
Jun 27 '08 #5
gw****@aol.com wrote:
On 20 Jun, 18:04, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"Kai-Uwe Bux" <jkherci...@gmx.netwrote in message

news:g3**********@aioe.org...
>>Angus wrote:
If I need to check if a pointer is valid I usually do a if(ptr) check
first.
But I have seen code such as this:
if((myptr) && (myptr->IsGreen())
Is this valid?
Yes.
Is it because evaluation is from left to right so that the if(ptr) bit
is evaluated first. Then only if pointer is valid is right function
called?
The formal reason is the guarantee in [5.14/1]:
The && operator groups left-to-right. The operands are both implicitly
converted to type bool (clause 4). The result is true if both operands
are
true and false otherwise. Unlike &, && guarantees left-to-right
evaluation: the second operand is not evaluated if the first operand is
false.
As a note, this is sometimes refered to as "short circuiting". It will also
work with ||
if ( cond1 || cond2 )

cond2 will only be evaluated if cond1 is false.

Note however that this is a special feature of the operators && and
||. If you do something like:

x = a() + b();

there is no guarantee which of a and b will be called first.
Also, if operator&&() or operator||() is overloaded, there is also no
such guarantee.
Jun 27 '08 #6
red floyd wrote:
Also, if operator&&() or operator||() is overloaded, there is also no
such guarantee.
Only criminals and madmen implement them otherwise.
Jun 27 '08 #7
peter koch wrote:
>if((myptr) && (myptr->IsGreen())

Is this valid?

Yes, this is guaranteed to work.
Nitpicking, but it's guaranteed to work only if myptr actually points
to an object of the proper type. If it points to garbage (which could be
achieved, for example, through reinterpret_cast), obviously UB will
happen. But yeah, this is just nitpicking.
Jun 27 '08 #8
if((myptr) && (myptr->IsGreen())
>
Is this valid?
Generally, it is valid. However, I have seen compiler options such as
"full evaluations of binary expressions". This option means that all
expressions are evaluated, even though the result is known without
calculating all of them.

My 2 cents
Jun 27 '08 #9
Yakov Gerlovin wrote:
>if((myptr) && (myptr->IsGreen())

Is this valid?

Generally, it is valid. However, I have seen compiler options such as
"full evaluations of binary expressions". This option means that all
expressions are evaluated, even though the result is known without
calculating all of them.
I really can't understand the reason for a compiler to offer such an
option. Not only does it break the standard, it probably breaks a lot of
code, and potentially makes code very inefficient. It's also completely
useless because if at some point in your code you really need to
evaluate both expressions, you can do that explicitly:

bool ret1 = expression1();
bool ret2 = expression2();
if(ret1 && ret2) ...

which also makes it quite clearer that evaluating both expressions is
intended, instead of being a hidden feature of an obscure compiler option.

That kind of option just doesn't make any sense whatsoever.
Jun 27 '08 #10
On 21 Jun., 11:37, Yakov Gerlovin <yakov.gerlo...@gmail.comwrote:
if((myptr) && (myptr->IsGreen())
Is this valid?

Generally, it is valid. However, I have seen compiler options such as
"full evaluations of binary expressions". This option means that all
expressions are evaluated, even though the result is known without
calculating all of them.

My 2 cents
I have not, but certainly using such an option makes the behaviour non-
compliant, and it also would break tons of code in existence. What
compiler are you talking about?
I have an understanding for other languages that prefer more freedom
in evaluating boolean expression - one obvious example is SQL. Also
Ada will normally guarantee evaluation of both sides. Special
operators are needed to avoid this (AND THEN resp. OR ELSE). In my
mind, this is the wrong default for a low-level language (and the
right one for a highlevel ditto).

/Peter
Jun 27 '08 #11
James Kanze wrote:
Because they would be impossible to specify: you would need to
call a function with two operands, but not have evaluated the
second under some undefined conditions (undefined, because
that's what the overload would define).
Clearly C++ needs some form of lazy evaluation... ;)
Jun 27 '08 #12
Greg Herlihy wrote:
On Jun 20, 3:08 pm, Noah Roberts <u...@example.netwrote:
>red floyd wrote:
>>Also, if operator&&() or operator||() is overloaded, there is also no
such guarantee.
Only criminals and madmen implement them otherwise.

You must mean that only criminals and madmen implement these overloads
-at all-.
No, I just didn't think about it clearly since I never do it.

I can imagine that you might want to, but I'd think twice about it any
time I might. Since you can't write it to have the same behavior, you
can't really call templates that use it. Probably better to explicitly
use some function call that you then override for basic types so you can
use your new templates with old types.
Jun 27 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: net | last post by:
I know how to make a page open itself at a certain size and location - I use: <html> <head><script language="Javascript"> window.resizeTo(370,220); window.moveTo(10,10); </script> <meta...
14
by: Timothy Madden | last post by:
Hello I have a linked list of object of a class. I thought it would be nice to have the destructor delete the whole list when I delete just the first element. I don't want to recursivly destroy...
28
by: PerryC | last post by:
Anyone know how to auto close the parent / opener window without confirmation? I have tried: <script> opener.window.close() </script> ----I put it in the child html page, and nothing...
9
by: WalterR | last post by:
This is my first time here, so there may be earlier relevant threads of which I am unaware. Though my experience with DB2 is not extensive, such as it is was under OS/390 or equ. My main...
2
by: Kristopher Wragg | last post by:
I'm having some serious problems with the TreeView control. I've got a control that inherits TreeView and has some methods that firstly create a TreeNode then does some recursive procedure to add...
6
by: rbutlerjr | last post by:
Hi, I've racked my brain for the last few hours trying to figure this one out. I have an array of language strings such as : $lang = array(); $lang = 'hello'; $lang = 'goodbye'; $lang = '1st';...
5
by: mmcd79 | last post by:
I built a VB.net application that makes use of a machine level DB connection string setting, and a user level starting location setting. The machine level setting and the default user based...
6
by: ssecorp | last post by:
def fib(n): def fibt(a, b, n): if n <= 1: return b else: return fibt(b, a + b, n - 1) if n == 0: return 0 else: return fibt(0, 1, n);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.