Another way (How not to write a loop)

Ray Butterworth rbutterworth at watmath.waterloo.edu
Tue Feb 16 01:09:15 AEST 1988


I just got hit by yet another way not to write a loop:

    auto size_t index;

    index = sizeof(array) / sizeof(array[0]);

    while (--index >= 0)
        blah blah blah

This looks wonderfully portable and works fine on the BSD 4.3 compiler
(and probably most others).

But on an ANSI compiler, (size_t) will be an unsigned integer
and the loop will go forever.

Lint will notice this problem, but only with the ANSI compiler
when it is probably too late.



More information about the Comp.lang.c mailing list