Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!utcsri!greg From: greg@utcsri.UUCP Newsgroups: comp.lang.c Subject: Re: Style [++i vs i++] Message-ID: <5068@utcsri.UUCP> Date: Mon, 13-Jul-87 00:07:39 EDT Article-I.D.: utcsri.5068 Posted: Mon Jul 13 00:07:39 1987 Date-Received: Mon, 13-Jul-87 01:40:33 EDT References: <17310@amdcad.AMD.COM> <2159@emory.uucp> <43@ghsvax.UUCP> <223@wrs.UUCP> <795@nu3b2.UUCP> Reply-To: greg@utcsri.UUCP (Gregory Smith) Organization: CSRI, University of Toronto Lines: 51 Summary: In article <795@nu3b2.UUCP> rwhite@nu3b2.UUCP (Robert C. White Jr.) writes: >In article <223@wrs.UUCP>, dg@wrs.UUCP (David Goodenough) writes: >> In article <43@ghsvax.UUCP> edk@ghsvax.UUCP (Ed Kaulakis) writes: >> > >> > Many compilers will materialize the (returned old value) of i++ even >> >when nobody wants it, but will do better with ++i. >> >> Ye gads! what braindamaged piece of software are you using?? - I've worked > > Pardon my foolish inquiry... isnt i++ SUPPOSED to return the old >value of i AND THEN increment it's stored value. Ed, if this isnt what >you mean ignore this and then post an example of the misfunctioning code, >I for one would love to see a murdered code fragment and credits for the >complier. [as a warning against purchase] The key is 'when nobody wants it'. And I wouldn't say "Many" compilers unless you use grotty micro compilers on a regular basis. Example: Small-C, for 8080, and its progeny C/80 from Software Toolworks: int i1,i2; /* my examples use externals. You don't want to see how autos are accessed on the 8080 */ func(){ i1= ++i2; LHLD i2 ; get it INX H SHLD i2 ; put it back SHLD i1 ; save result i1= i2++; LHLD i2 INX H SHLD i2 DCX H ; get prev value (i++ treated as (++i)-1 ) SHLD i1 The catch is that the statements "++i2" and "i2++" generate the same code without the SHLD i2. So in the case of "i2++" there is a wasted DCX H, to give the (unused) value of i2++. This is because the code generator uses a 'building block' technique, where each operation produces a canned piece of code, which must leave a value in a register just in case the op is part of a larger expression. For a much more gruesome example of building-block coding, take a look at the code produced for IF(I.LE.J)... in MicrSoft FORTRAN-80 (BTW it includes an incorrect transformation to ((I-J).LE.0) ). Ah, the 8080... and you can reassemble this stuff to run on the 80386!!! -- ---------------------------------------------------------------------- Greg Smith University of Toronto UUCP: ..utzoo!utcsri!greg Have vAX, will hack...