This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Relaxing -Wsign-compare
- To: Morten Welinder <terra at diku dot dk>
- Subject: Re: Relaxing -Wsign-compare
- From: Neil Booth <neil at daikokuya dot demon dot co dot uk>
- Date: Thu, 20 Sep 2001 19:41:45 +0100
- Cc: gcc at gcc dot gnu dot org
- References: <20010920154527.26304.qmail@ntyr.diku.dk>
Morten Welinder wrote:-
> IMHO, the -Wsign-compare is a bit too agressive to be really useful.
> Consider this example:
>
> -----------------------------------------------------------------------------
> struct {
> int a, b;
> } foo [] = {
> {1,2}, {3,4}
> };
>
> int
> main (int argc, char **argv)
> {
> int i;
>
> for (i = 0; i < sizeof (foo) / sizeof (foo[0]); i++)
> ;
>
> return 0;
> }
> -----------------------------------------------------------------------------
I don't think you've chosen a good example -- I'd claim that i should
be an unsigned int. Personally, it bugs me that people default to
loops with int, when 95% of the time they're looping with unsigned
quantities. It can even give you better code to be looping unsigned,
since some arithmetic operations can be more efficiently encoded if
the compiler knows they are on unsigned quantities.
And signed lengths, for that matter.
> signed.c: In function `main':
> signed.c:12: warning: comparison between signed and unsigned
>
> A similar warning can be had with typical MIN and MAX macros when
> used with an unsigned expression and an explicit integer, say
> MAX (expr, 42).
I think Kaveh has done some work on the compiler to improve this area
in 3.1.
Neil.