Re: [Code] Brainteaser

From: Martijn Schoemaker (martijn@osp.nl)
Date: 11/21/01


John wrote:

> What is the difference between:-
> if (!str || !*str) return;
> and
> if (!str) return;
> else if (!*str) return;

My first hunch is that 'lazy evaluation' is not used. This means that
the compiler probably fully evaluates every boolean term while the
outcome can already be determined by evaluating part of the terms.

Im plain english : In normal circumstances, with no efficiency you
would first evaluate !str (returns true) then !*str (crashes! but assume
it outputs true) then do the boolean evaluation : true || true = true and

thus return.

With lazy evaluation we know that when we use Or statements that
if any of the terms evaluates to true, the complete boolean comparison
will always be true. So : evaluate !str (returns true), Ah, no need to
look further since the operator is || and immediately return (and in your

case dont crash due to nullpointer dereferencing).

Try to check your compiler settings for 'lazy' or 'full' boolean
evaluation. Most compilers nowadays use lazy evaluation to
improve performance. Some allow you to switch this off because
in some rare circumstances lazy evaluation and optimizing may
result in strange behaviour.

Cheers,
Aragorn

Try : imagica.net 4000

--
------ WARNING: This signature contains a VIRUS ! -------
- SHLRUIOHUIOWHLNNMSKHKDLWINDOWSJHFHKJLLUIHEKJLNDHKKJHL -
---------------------------------------------------------

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/06/01 PST