Re: Style question: comparison between signed and unsigned?

linux kernel account (linker@nightshade.z.ml.org)
Mon, 22 Sep 1997 21:01:49 -0400 (EDT)


On Mon, 22 Sep 1997, Linus Torvalds wrote:

[snip]
> So to get rid of that _spurious_ warning, I'd have to change the test into
> something like:
>
> if (i < (int) sizeof(struct pkthdr))
> return SHORT_PACKET;
>
> Quite frankly, anybody who claims that the extra cast is a good thing
> should be shot on the spot - the extra cast is an abomination and has _no_
> redeeming features except to shut up a warning from a compiler that thinks
> it knows better than the programmer.

Linus, you may be the programming God that I could never hope to be, but
I feel there is a use for that jumping-through-hoops: It encourages the
programmer to take a look at a possibly bad situation (mixing types) and
decided if the code is actually doing the right thing. Mixing types
improperly can cause bug that only show up under rare circumstances,
something the you dont want in a program... So it's good to have a sanity
check.

> Anybody who thinks that their compiler is smarter than they are probably
> disagrees with me. But type-casts are the _single_ most dangerous thing
> you can do in C, and a compiler that _encourages_ you to add spurious type
> casting for no gain is a bad compiler.

I dont believe that it's smarter then I and it's certantly not smarter
then you, but the compiler does read every line of code with a computers
exacting precision. Can you say you do the same? I believe it is bad to
mindlessly cast, but it is good to be asked to confirm when you do
something that could cause bad and strange problems.