---------- Forwarded message ----------
Date: Thu, 28 Feb 2002 13:21:46 +1300 (NZDT)
From: Richard A. O'Keefe <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED], [EMAIL PROTECTED]
Subject: Re:  Forwarded mail....

eazzy <[EMAIL PROTECTED]> wrote:

        Does "SPLint" have a means to take out warning to the following
        programs?

        void foo(void) {
            unsigned short cnt;

            for (cnt = 0; cnt < 0x10000; cnt++) {
                printf("cnt=%u\n", cnt);
            }
        }

Yes, it does.  It's called a 'cast'.

        void foo(void) {
            unsigned short cnt;

            for (cnt = 0; (unsigned int)cnt < 0x10000u; cnt++) {
                printf("cnt=%u\n", (unsigned int)cnt);
            }
        }

Of course, this doesn't work if int is 16 bits, so it's a good idea
to stick in
        #include <limits.h>
        #if INT_MAX < 0x10000
        #error "Requires INT_MAX >= 0x10000"
        #endif

Since 'int' is supposed to be the most efficient size for an integer,
it's not clear to me why 'cnt' is unsigned _short_ in the first place.

        #include <limits.h>
        #if UINT_MAX < 0x10000u
        #error "Requires UINT_MAX >= 0x10000u"
        #endif

        void foo(void) {
            unsigned int cnt;

            for (cnt = 0; cnt < 0x10000u; cnt++) {
                printf("cnt=%u\n", cnt);
            }
        }

This doesn't need any casts and the types are exactly right everywhere.

Is it a coincidence that we've had two people whose first thought is
"how do I shut splint up" rather than "how do I write my code so that
splint has nothing to say"?  Both of these examples were ones that
could be written more clearly FOR HUMAN READERS entirely at the C level
without splint annotations or options at all.


Reply via email to