Re: C and side-effects

From: Michael Bacarella (mbac@nyct.net)
Date: Wed May 03 2000 - 18:19:36 EST


> while ((mm->swap_cnt << 2 * (i + 1) < max_cnt) && i++ < 10)
> /* nothing */;
>
> 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?

AND conditionals are evaluated left to right. If any part of it evaluates
to false then entire statement is false so there is no need to continue
evaluating it.

When mm->swap_cnt << 2 * (i + 1) is false, the loop terminates and i++ <
10 is not executed.

> I'd suggest
> while (mm->swap_cnt << 2 * (i + 1) < max_cnt)
> {
> int j = i++;
> if (!(j < 10))
> break;
> }

The first example is easier for me to understand than the second one, just
because there's less visual noise. Although I'd be quite the sucker if
my explanation was wrong.

-MB

-
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