This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Relaxing -Wsign-compare


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]