This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: constant signed/unsigned comparison warning
- To: erik at arbat dot com (Erik Corry)
- Subject: Re: constant signed/unsigned comparison warning
- From: "Matthias Urlichs" <smurf at noris dot de>
- Date: Thu, 25 Sep 1997 09:22:55 +0200 (Funky)
- Cc: smurf at noris dot de, torvalds at transmeta dot com, egcs at cygnus dot com
Hi,
Erik Corry wrote:
>
>> Apparently, especially ugly for people with other compilers who actually
>> might run into problems when GCC does something like this.
>
>Why does this cause problems? People with other compilers can
>just disable signed/unsigned warnings entirely. Gcc users get
>just the warnings that (might) matter, and none of the others.
>Gcc has always been great at warnings.
The problem has been beaten to death, actually, but what the hell...
int i = -15;
if(i < sizeof(i)) printf("Neat") else printf("You forgot something");
Now, my idea was that the above would read as if(i<4). The Standard however
mandates that the above reads as a comparison of (unsigned)i, which is
rather large, to 4, which would be A Bad Thing.
I (reluctantly) have to agree with others here that changing semantics just
because of using GCC is a Bad Thing.
On the other hand, consider this code snippet:
int i = something_random();
unsigned j = something_equally_random();
if (i < 0 || i < j) printf("smaller");
Now, this test obviously says that the programmer has thought about the
problem. It should be possible for GCC's optimizer to notice that, and SHUT
UP since the above code can't possibly be buggy.
On the other hand, with
if (i < (int) j) printf("smaller");
or
if ( (unsigned) i < j) printf("smaller");
the problem has been hidden; it's just the warning which has been disabled.
I don't want to require the programmer to do two different things here (one
to write the program correctly and another to tell GCC about it). It's far
easier, for the programmer, to either forget the check ("the cast takes
care of the problem") or to omit -W altogether. Just look at the real
world.
Let's face it, C is not a strongly-typed language. If it were, we would
be able to have an operator< (int,unsigned) which does the Right Thing.
Unfortunately, that doesn't seem to be possible.
--
Matthias Urlichs
noris network GmbH