Another way (How not to write a loop)

Thomas Truscott trt at rti.UUCP
Sat Feb 20 14:36:45 AEST 1988


> In article <16982 at watmath.waterloo.edu> rbutterworth at watmath.waterloo.edu (Ray Butterworth) writes:
> -> >     auto size_t index;
> -> >     index = sizeof(array) / sizeof(array[0]);
> -> >     while (--index >= 0)
> -... this is yet another
> -way in which otherwise correct code will break when it is ported
> -to an ANSI compiler (as I found out the hard way).

The Gould compiler says "warning: unsigned >= always true".

In article <7278 at brl-smoke.ARPA>, gwyn at brl-smoke.ARPA (Doug Gwyn ) writes:
> The fact that the result of sizeof has an unsigned integral type is
> not new with ANSI C.

Yes, nor is it the cause of the infinite loop.

By the way, the suggested fix:
	while (index-- > 0)
causes the Gould compiler to say:
	"warning: degenerate unsigned comparison"
which is reasonable since the fix is misleading.  Better would be:
	while (index-- != 0)

If your compiler does not do these things (and much much more!)
complain to the vendor.  Start writing SPRs!!
You can spend the time now, or later ("the hard way").
	Tom Truscott



More information about the Comp.lang.c mailing list