Source-Changes-D archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: CVS commit: src/sys/uvm



> Date: Mon, 6 Jul 2026 00:14:59 +0900
> From: Takashi YAMAMOTO <yamt9999%gmail.com@localhost>
> 
> @@ -900,7 +897,7 @@ uvm_swap_stats(char *ptr, int misc,
>         error = 0;
>         count = 0;
>         sp = sdps;
> -       while (misc-- > 0) {
> +       while (misc --> 0) {
> 
> this style change is quite surprising to me. it it intentional?

This is the best operator in C!  --> is the `goes down to' operator.

If you're iterating _forward_ over an array of length N, the usual
idiom is:

	for (i = 0; i < N; i++) {
		... i ...
	}

This loop runs for exactly N iterations; inside the loop body, i is
always a valid index into the array; and it works with both signed and
unsigned arithmetic, and with arbitrary [start,end) intervals instead
of [0,N).

What if you want to iterate _backward_ over an array of length N?

You could try something like

	for (i = N; i > 0; i--) {
		... i - 1 ...
	}

but then your code is full of -1 or +1 that are hard to follow in the
body and make it look like there are fencepost errors everywhere.  You
could maybe fix that with

	for (i = N - 1; i >= 0; i--) {
		... i ...
	}

but now the loop head looks like it has fencepost errors, and it only
works if i is signed.  You could use

	for (i = N; --i >= 0;)
or
	for (i = N; i-- > 0;)

but who can remember whether post-increment or pre-increment is
correct, and whether to use >= or > with them, and which way makes
fencepost errors, and which one works in unsigned arithmetic?

Better approach, much more memorable and easy to read: use the `goes
down to' operator!

	for (i = N; i --> 0;) {
		... i ...
	}

As with forward iteration, this loop runs for exactly N iterations;
inside the loop body, i is always a valid index into the array; and it
works with both signed and unsigned arithmetic, and with arbitrary
[start,end) intervals instead of [0,N).


Home | Main Index | Thread Index | Old Index