short circuit evaluation & side-effects

mason at tmsoft.UUCP mason at tmsoft.UUCP
Thu Feb 5 02:23:48 AEST 1987


In K&R pg. 184 it says: "...Otherwise the order of evaluations of expressions
is undefined.  In particular the compiler considers itself free to compute
subexpressions in the order it believes most efficient, even if the
subexpressions involve side effects.  The order in which side effects take place
is undefined."
    As I understand it this means the results of:
(1)	x = *i++ * (1 - *i++);
is unpredicatable.  ANSI added the unary + to fix this (as well as for numerical
analysis (i.e. floating point) calculations) so that:
(2)	x = +(*i++ * (1 - *i++));
does what one would expect.  I have seen notes here that suggested that (1)
could be equivalent to:
(3)	x = *i * (1 - *i); i += 2;

Does the unary + force the side effects to happen where expected?  Is unary +
still in the ANSI standard proposal?

The above quote would appear to require side effects to take place (although
the order is undefined) even if short circuiting were to be done (except of
course in the case of || and &&)
-- 
	../Dave Mason,	TM Software Associates	(Compilers & System Consulting)
	..!{utzoo!ryesone seismo!mnetor utcsri!ryesone}!tmsoft!mason



More information about the Comp.lang.c mailing list