Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!ico!ism780c!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.std.c Subject: Re: pointers & order of execution Keywords: pointer subtraction, order of execution, realloc Message-ID: <13757@haddock.ima.isc.com> Date: 19 Jun 89 23:14:04 GMT References: <920@tukki.jyu.fi> <921@tukki.jyu.fi> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Boston Lines: 18 In article <921@tukki.jyu.fi> tarvaine@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@haddock.isc.com), The Walking Lint