C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

strange interview question
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
me
Guest





PostPosted: Tue Oct 26, 2004 10:51 pm    Post subject: strange interview question Reply with quote



Guys

I recently had an interview question that asked (in C++):

Are these boolean expressions equivalent?

( a && b->c )

and

( b->c && a )

No other information was given.

My first reaction was no, as the value of the first operand could have a
side effect. Is this correct?

I started to think about other cases. If a and c are POD types ( or
class types with suitable conversions to bool that have no side effects
), then yes, they are equivalent. But is it possible that a and/or c
could be function pointers retruning booleans? Or is that syntatically
not possible?

Unfortunately the question was a simple yes/no question, so I couldnt go
into detail. But it set me thinking....

cheers

wjp

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Wendy E. McCaughrin
Guest





PostPosted: Wed Oct 27, 2004 4:08 am    Post subject: Re: strange interview question Reply with quote



me <me (AT) me (DOT) com> wrote:
: Guys

: I recently had an interview question that asked (in C++):

: Are these boolean expressions equivalent?

: ( a && b->c )

: and

: ( b->c && a )

: No other information was given.


The first expression's result = (if a == TRUE then b->c else FALSE),
and the second expr's result = (if b->c == TRUE then a else FALSE).
Now, what if a == FALSE but b->c is undefined?



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Antonio Maiorano
Guest





PostPosted: Wed Oct 27, 2004 4:09 am    Post subject: Re: strange interview question Reply with quote



The answer they expect is probably 'no' because of short-circuit evaluation.
If 'a' is false in the first case, then b->c will never execute; in the
latter case, if b->c is false, 'a' is never evaluated.

--
Antonio Maiorano
Software Engineer, Electronic Arts
E-Mail: m a i o r a n o AT v i d e o t r o n DOT c a


"me" <me (AT) me (DOT) com> wrote

Quote:
Guys

I recently had an interview question that asked (in C++):

Are these boolean expressions equivalent?

( a && b->c )

and

( b->c && a )

No other information was given.

My first reaction was no, as the value of the first operand could have a
side effect. Is this correct?

I started to think about other cases. If a and c are POD types ( or
class types with suitable conversions to bool that have no side effects
), then yes, they are equivalent. But is it possible that a and/or c
could be function pointers retruning booleans? Or is that syntatically
not possible?

Unfortunately the question was a simple yes/no question, so I couldnt go
into detail. But it set me thinking....


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ioannis Vranos
Guest





PostPosted: Wed Oct 27, 2004 4:12 am    Post subject: Re: strange interview question Reply with quote

me wrote:

Quote:
Guys

I recently had an interview question that asked (in C++):

Are these boolean expressions equivalent?

( a && b->c )

and

( b->c && a )

No other information was given.

My first reaction was no, as the value of the first operand could have a
side effect. Is this correct?

I started to think about other cases. If a and c are POD types ( or
class types with suitable conversions to bool that have no side effects
), then yes, they are equivalent. But is it possible that a and/or c
could be function pointers retruning booleans? Or is that syntatically
not possible?

Unfortunately the question was a simple yes/no question, so I couldnt go
into detail. But it set me thinking....


It depends what a,b,c are exactly, however strictly speaking are not
equivalent since they are evaluated from left to right, so if the left
is false the right is not checked.


In terms of the expression evaluation result however, if anything sneaky
is not going on, they are the same.



--
Ioannis Vranos

http://www23.brinkster.com/noicys

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Bob Hairgrove
Guest





PostPosted: Wed Oct 27, 2004 2:06 pm    Post subject: Re: strange interview question Reply with quote

On 26 Oct 2004 18:51:07 -0400, me <me (AT) me (DOT) com> wrote:

Quote:
Guys

I recently had an interview question that asked (in C++):

Are these boolean expressions equivalent?

( a && b->c )

and

( b->c && a )

No other information was given.

My first reaction was no, as the value of the first operand could have a
side effect. Is this correct?

I started to think about other cases. If a and c are POD types ( or
class types with suitable conversions to bool that have no side effects
), then yes, they are equivalent. But is it possible that a and/or c
could be function pointers retruning booleans? Or is that syntatically
not possible?

Unfortunately the question was a simple yes/no question, so I couldnt go
into detail. But it set me thinking....

If b->c is a function pointer, it would have to be dereferenced,
either by adding () or with * before it would run anything.

b->c might execute code if a custom conversion operator to bool has
been defined, however.

--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed Oct 27, 2004 2:06 pm    Post subject: Re: strange interview question Reply with quote

me <me (AT) me (DOT) com> wrote


Quote:
I recently had an interview question that asked (in C++):

Are these boolean expressions equivalent?

( a && b->c )

and

( b->c && a )

No other information was given.

My first reaction was no, as the value of the first operand could have
a side effect. Is this correct?

I suspect that the answer wanted was no, if the left hand side of the
expression is false, then the right hand side is never evaluated. On
sees this a lot in things like:

( p != NULL && p->something )

Swapping the two sides will typicaly result in undefined behavior -- a
program crash on many systems.

Quote:
I started to think about other cases. If a and c are POD types ( or
class types with suitable conversions to bool that have no side
effects ), then yes, they are equivalent. But is it possible that a
and/or c could be function pointers retruning booleans? Or is that
syntatically not possible?

Imagine the following :

struct S { bool c ; } ;
S* b = NULL ;
struct A { operator bool() { return b != 0 } } ;
A a ;

In this case, the first expression if legal, and returns false. The
second is illegal, results in undefined behavior, and will, in fact,
core dump on my machines.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maxim Yegorushkin
Guest





PostPosted: Wed Oct 27, 2004 2:20 pm    Post subject: Re: strange interview question Reply with quote

Antonio Maiorano <do (AT) not (DOT) use> wrote:

Quote:
The answer they expect is probably 'no' because of short-circuit evaluation.
If 'a' is false in the first case, then b->c will never execute; in the
latter case, if b->c is false, 'a' is never evaluated.

Am I right if I say, that there is no short-circuit evaluation for user defined && and ||?

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Antoun Kanawati
Guest





PostPosted: Wed Oct 27, 2004 2:23 pm    Post subject: Re: strange interview question Reply with quote

me wrote:
Quote:
Guys

I recently had an interview question that asked (in C++):

Are these boolean expressions equivalent?

( a && b->c )

and

( b->c && a )

No other information was given.

They're definitely not equvialent. The first means:

a ? b->c : false

The second means:

b->c ? a : false

In the first case, the pointer dereference is conditional. In the
second case it is not.

For example, if you pick the value a == false, and b == null, the first
form is well defined, while the second is undefined (null pointer
access).
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Mickey Moore
Guest





PostPosted: Wed Oct 27, 2004 2:24 pm    Post subject: Re: strange interview question Reply with quote

In article <2u6i2uF2776p0U1 (AT) uni-berlin (DOT) de>, [email]me (AT) me (DOT) com[/email]
says...
Quote:
I recently had an interview question that asked (in C++):

Are these boolean expressions equivalent?

( a && b->c )

and

( b->c && a )

No other information was given.

My first reaction was no, as the value of the first operand could have a
side effect. Is this correct?

I started to think about other cases. If a and c are POD types ( or
class types with suitable conversions to bool that have no side effects
), then yes, they are equivalent. But is it possible that a and/or c
could be function pointers retruning booleans? Or is that syntatically
not possible?

Unfortunately the question was a simple yes/no question, so I couldnt go
into detail. But it set me thinking....

Your thinking and the other answers given seem correct,
but matters could in theory be murkier still: It would
also depend on whether operator&& was overloaded. If
the overloaded operator&& didn't try to follow the
expected semantics, absolutely all bets are off.

(You can truly come up with some Bizzaro World calls
this way: a and b->c could have different types, which
would invoke different overloads of operator&&, which
might have different return types.)

Of course, back in the real world, overloaded operators
should follow the normal semantics, which means you
shouldn't overload operator&&, because you can't obtain
the normal (short-circuit) semantics. (See Meyers 2:7.)

----------------------------------------
Mickey Moore

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Walt Karas
Guest





PostPosted: Thu Oct 28, 2004 1:44 pm    Post subject: Re: strange interview question Reply with quote

me <me (AT) me (DOT) com> wrote

Quote:
Guys

I recently had an interview question that asked (in C++):

Are these boolean expressions equivalent?

( a && b->c )

and

( b->c && a )

No other information was given.

My first reaction was no, as the value of the first operand could have a
side effect. Is this correct?

I started to think about other cases. If a and c are POD types ( or
class types with suitable conversions to bool that have no side effects
), then yes, they are equivalent. But is it possible that a and/or c
could be function pointers retruning booleans? Or is that syntatically
not possible?

Unfortunately the question was a simple yes/no question, so I couldnt go
into detail. But it set me thinking....

cheers

wjp

If either a or b->c is not of type bool, I believe it's also possible
that && could resolve to (possibly two different) non-built-in versions
of operator &&.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Neil Tardella
Guest





PostPosted: Thu Oct 28, 2004 1:48 pm    Post subject: Re: strange interview question Reply with quote

Right,
I think this is a practical question. Suppose b is a null pointer? A
simple safeguard i've seen would be something like
(b && b->c)..
-n


"Wendy E. McCaughrin" <wemccaug (AT) bluestem (DOT) prairienet.org> wrote

Quote:
me <me (AT) me (DOT) com> wrote:
: Guys

: I recently had an interview question that asked (in C++):

: Are these boolean expressions equivalent?

: ( a && b->c )

: and

: ( b->c && a )

: No other information was given.


The first expression's result = (if a == TRUE then b->c else FALSE),
and the second expr's result = (if b->c == TRUE then a else FALSE).
Now, what if a == FALSE but b->c is undefined?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Hyman Rosen
Guest





PostPosted: Thu Oct 28, 2004 1:54 pm    Post subject: Re: strange interview question Reply with quote

me wrote:
Quote:
If a and c are POD types , then yes, they are equivalent.

Of course they are not. The simplest case is that b might
be an invalid or null pointer, and a is the flag which tells
you whether b may be used.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Stuart Gerchick
Guest





PostPosted: Thu Oct 28, 2004 1:58 pm    Post subject: Re: strange interview question Reply with quote

The result from both should be identical. However remember that if
the first of the operands is false the rest will not be evaluated. so
fi b->c has a side effect it might not happen.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Peter A. Kerzum
Guest





PostPosted: Thu Oct 28, 2004 1:59 pm    Post subject: Re: strange interview question Reply with quote

me <me (AT) me (DOT) com> wrote

Quote:
Guys

I recently had an interview question that asked (in C++):

Are these boolean expressions equivalent?

( a && b->c )

and

( b->c && a )

No other information was given.

My first reaction was no, as the value of the first operand could have a
side effect. Is this correct?

I started to think about other cases. If a and c are POD types ( or
class types with suitable conversions to bool that have no side effects
), then yes, they are equivalent. But is it possible that a and/or c
could be function pointers retruning booleans? Or is that syntatically
not possible?

AFAIK this is not possible - any execution requires brackets operator.
But it is possible for b to have overloaded -> operator that can implement
any side effects. So the answer is definitely no.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ron Natalie
Guest





PostPosted: Thu Oct 28, 2004 2:00 pm    Post subject: Re: strange interview question Reply with quote

me wrote:
Quote:
Guys

I recently had an interview question that asked (in C++):

Are these boolean expressions equivalent?

( a && b->c )

and

( b->c && a )

No other information was given.

My first reaction was no, as the value of the first operand could have a
side effect. Is this correct?


The answer is no. The first expression may prevent undefined behavior
where the second one will not.

imagine:
bool a = false;
struct {
bool c;
}* b = 0;

now evaluate the two expressions above.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.