Heroic failures (q = q++)

John F. Woods jfw at ksr.com
Wed Jun 26 06:08:44 AEST 1991


vulcan at uiuc.edu (EvilTwin) writes:
>In article <902 at adimail.UUCP> tel at adimail.UUCP (Terry Monks) writes:
>>(Peter van der Linden (linden at adapt.Eng.sun.com)) scripsit:
>>>                 q = q++;

EWW!

>A co-worker and I tried the above on our respective machines.  He on an IBM
>70/386 using Borland C++, I on a MicroVAX II using DEC's C version 3.1.  He
>received a result of 6, I one of 5.  Why is this?

Why is this?  You used two different compilers to compile an undefined
expression.  Why 5 and 6?  5 is the reasonable result of compiling into code
like
	TMP := q	; value of postincrement is old value
	q := q+1	; do postincrement
	q := TMP	; do assignment
and 6 corresponds to
	TMP := q	; value of postincrement is old value
	q := TMP	; do assignment
	q++		; take care of postincrements left over in expression
Depending on the actual hardware and software architecture, either of these
is a perfectly reasonable approaches to take (of course, a decent compiler
would probably be able to reduce them to zero or one machine instructions
respectively).

As to why it is undefined, read The Standard and the FAQ, alternately, until
either you reach enlightenment or your head explodes.  Note that because it is
*undefined*, the compiler was perfectly at liberty to generate
	q := 666	; special marker for evil constructs
or
	JSR _cpu$detonate
or any other damned thing it liked.



More information about the Comp.lang.c mailing list