home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.scheme:2871 comp.lang.c:19281
- Newsgroups: comp.lang.scheme,comp.lang.c
- Path: sparky!uunet!microsoft!wingnut!russpj
- From: russpj@microsoft.com (Russ Paul-Jones)
- Subject: Re: applying or
- Message-ID: <1993Jan06.002044.795@microsoft.com>
- Date: 06 Jan 93 00:20:44 GMT
- Organization: Microsoft Corporation
- References: <1993Jan1.184655.7023@viewlogic.com>
- Lines: 67
-
- In article <1993Jan1.184655.7023@viewlogic.com> josh@viewlogic.com (Josh Marantz) writes:
- >
- > [about short circuit evaluation of && and || (C syntax)]
- >
- >I was sort of glad to move to C/Scheme, where this short-circuiting
- >was guaranteed, although I never thought about the compiler
- >optimizations that the short-circuit guarantee prevents! Would it be
- >legal for a C or Scheme compiler to avoid the short-circuiting if it
- >could prove there were no side effects involved, and if it looked like
- >a fruitful optimization?
-
- I can't speak for the Scheme language, but side effects are not the
- only problem in C; short circuit evaluation is often used to
- prevent "bad" runtime behavior.
-
- In the subprogram:
-
- if (a) && (b)
- c;
-
- where a, b, and c stand for legal C expressions, the programmer
- may rely on the fact that b should only be evaluated if a is
- non-zero. If a == 0, then b is allowed to be a "bad"
- expression (e.g. division by zero, dereference of an invalid pointer
- or out-of-bounds array arithmetic, a call to a function with invalid
- parameters, arithmetic overflow) without affecting the behavior of
- the rest of the program.
-
- Even if b is just a simple variable, if it is not initialized there
- may be problems (I can't find if Standard C categorizes access
- of an uninitizalized object as "harmless" or not).
-
- So, there are some instances where this optimization might be made:
-
- b = 4;
- if (a*7)/3 > 100/a || b
- fred();
-
- I would say that fred() may be called without evaluating the silly
- expression, even if a == 0. But I would suspect that it would take
- a fairly clever compiler to safely find many of these instances.
-
- On another note, it is good C programming practice to do a little
- optimization as a programmer in taking advantage of short-circuit
- evaluation. For example, if the expressions a and b are really
- independent, without side effects, and of roughly equal cost to
- evaluate, but b == 0 is more likely than a == 0, I would write:
-
- if (b) && (a)
- c;
-
- I would probably not be too excited about a compiler that evaluated
- a first to find it non-zero most of the time.
-
- If this has no relevance to Scheme, I'd suggest limiting followups
- to comp.lang.c.
-
- >--
- >Joshua Marantz You make my life and times
- >Viewlogic Systems, a book of bluesy Saturdays
- >josh@viewlogic.com And I have to choose...
-
- -Russ Paul-Jones
- russpj@microsoft.com
- I have no connection with the compiler folk here, and I have no idea
- what our compiler does with these operators. And, I'm not in any
- way speaking for my employer.
-