Re: C and side-effects

From: Michael Poole (poole@graviton.subatomic.org)
Date: Wed May 03 2000 - 20:27:50 EST


"Robert M. Love" <rml@tech9.net> writes:

> > Rik van Riel wrote:
> >
> > > I'm not that good at C, mas aren't expressions with side-effects
> > > ill-defined? How can we be sure (short of tracking the gcc
> > > mailing lists) that i will be incremented after the left part of
> > > the test?
> >
> > Because it is defined in the C standard?
> >
> > Rik
>
> maybe im reading the thread wrong, but Cesar is saying the code
> while ((mm->swap_cnt << 2 * (i + 1) < max_cnt) && i++ < 10)
> does not guarentee that the expression to the left of && occurs prior to
> "i++" -- and he is Right,

No, he is not right.

> because the C standard does *not* specify this. proof is in K&R, where they
> explicitly mention compiler design or optimization can change compound
> statement ordering in the resulting machine code. thus, i agree an
> alternative should be considered in the kernel.

Logical operators are not compound statements. Compound statements
are separated by commas, as in: "for (i=0, j=1; i<5; i++) ;" where the
compiler is free to put "i=0" or "j=1" first. Logical operators are
defined in C to do left-to-right evaluation with short-circuiting.

This is only one of the oldest C (and C++, and Java) idioms in the book.

Michael

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Sun May 07 2000 - 21:00:13 EST