short circuit evaluation
chris at mimsy.UUCP
chris at mimsy.UUCP
Mon Jan 26 12:17:54 AEST 1987
In article <14479 at amdcad.UUCP> tim at amdcad.UUCP (Tim Olson) writes:
>What about if either the left side expression or the right side
>expression [of an otherwise short-circuitable expression] contained
>a side-effect (or a procedure call, which also may have a side-
>effect)? These cannot be short-circuited when bit-wise operators
>are used.
c = *p++ & *q++;
/* are both p and q always incremented? */
I can find no promise in K&R that bitwise expressions are not short
circuited even in the presence of side effects. The ANSI draft
may have more to say. In any case, I would advise not counting on
full evaluation. It is even conceivable that a compiler might
generate
if ((c = *p) != 0)
c &= *q;
p++;
q++;
which, if p and q point to device registers, is not the same!
In general, existing C compilers will not attempt to optimise
away the AND above if *p++ (or *q++) is zero, simply because the
`C philosophy' is that if you *meant*
c = *p++ ? p[-1] & *q++ : 0;
you would have written that. But is it mandated? I cannot say.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP: seismo!mimsy!chris ARPA/CSNet: chris at mimsy.umd.edu
More information about the Comp.lang.c
mailing list