pointers & order of execution

Karl Heuer karl at haddock.ima.isc.com
Tue Jun 20 09:14:04 AEST 1989


In article <921 at tukki.jyu.fi> tarvaine at tukki.jyu.fi (Tapani Tarvainen) writes:
>[c marks a place in buffer b, which we want to realloc; old value in t]
>/*2*/	c = b + (c - t);
>Is this guaranteed to work, or is the compiler free to rearrange it as
>	c = (b - t) + c;
>even though b - t is illegal (and fails)?

Yes, this is guaranteed to work; parens must be honored in ANSI C.  Any such
rearrangement is now legal only via the as-if rule, which requires that the
rewrites be transparent to the user.  Thus, this optimization would be legal
for a compiler on a flat architecture (e.g. a pdp11 or vax), but not for a
segmented machine if the value (b-t) is not representable.

Similarly, lacking information about the possible values of j and k, an
integer expression like "i=(i-j)+k" cannot be optimized into "i+=(k-j)" unless
the integer-overflow trap is disabled.  (Which it usually is.)

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.std.c mailing list