Question about the new standard

drw at cullvax.UUCP drw at cullvax.UUCP
Thu Feb 19 02:50:25 AEST 1987


I've got a question about the new standard.  (Please discuss this only
within the context of the standard, or possible desirable
modifications of it.  I don't want to hear "well, *my* compiler does
it this way...")  Consider the code:

	char *p;
	int i;

	i = *p++ == *p++;

As far as I can tell, it is legitimate to increment p only once.  The
reasoning is: Evaluations of subexpressions are permitted to be
interleaved in any way, except (1) a subexpression whose operator has
a sequence point must not be interleaved with any other evaluation,
and (2) the result of an assignment or prefix inc/decrement must be
stored into its target before the resulting value is used.  Since the
above expression has no sequence points, we can evaluate it in the
following order:

	  1 2 3  4  5 6
	i = *p++ == *p++

A. Calculate p+1 (for 3)
B. Calculate p+1 (for 6)
C. Store p+1 (from A) in p (for 3)
D. Store p+1 (from B) in p (for 6)
E. Indirect (for 2 and 5)
F. Compare (for 4)
G. Assign (for 1)

In fact, the same reasoning works for "*++p == *++p", because the
above sequence of (abstract) execution gets the incremented values
stored before they are used for indirection.

Dale
-- 
Dale Worley		Cullinet Software
UUCP: ...!seismo!harvard!mit-eddie!cullvax!drw
ARPA: cullvax!drw at eddie.mit.edu



More information about the Comp.lang.c mailing list