linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Advice on Unsigned Types
@ 2001-08-31  2:11 John Kacur
  2001-09-05  1:00 ` H. Peter Anvin
  0 siblings, 1 reply; 2+ messages in thread
From: John Kacur @ 2001-08-31  2:11 UTC (permalink / raw)
  To: linux-kernel

Well, the min/max discussion has been educational if nothing else.
There is very good book called "Expert C Programming - Deep C Secrets",
by Peter Van Der Linden. (with a picture of a fish on the cover.) He
discusses the changes from K&R to ANSI C and quotes from the ANSI C
manual on Characters and Integers (the integral promotions) and on Usual
Arithmetic Conversions. An example he uses to show a subtle bug that
might occur if one doesn't think about these issues, is when using
sizeof, which could be hidden away in a macro. sizeof returns an
unsigned value. If you compare a negative int to the result of a sizeof
operation, it could be converted to an unsigned int and yield a large
positive number.

The advice the author give on Unsigned Types is:
"Avoid unnecessary complexity by minimizing your use of unsigned types.
Specifically, don't use an unsigned type to represent a quantity just
because it will never be negative (e.g."age" or "national debt").
Use a signed type like int and you won't have to worry about boundary
cases in the detailed rules for promoting mixed types.
Only use unsigned types for bitfields or binary masks. Use casts in
expressions, to make all the operands signed or unsigned, so the
compiler does not have to choose the result type."

John Kacur

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Advice on Unsigned Types
  2001-08-31  2:11 Advice on Unsigned Types John Kacur
@ 2001-09-05  1:00 ` H. Peter Anvin
  0 siblings, 0 replies; 2+ messages in thread
From: H. Peter Anvin @ 2001-09-05  1:00 UTC (permalink / raw)
  To: linux-kernel

Followup to:  <3B8EF269.BF457C7F@home.com>
By author:    John Kacur <jkacur@home.com>
In newsgroup: linux.dev.kernel
> 
> The advice the author give on Unsigned Types is:
> "Avoid unnecessary complexity by minimizing your use of unsigned types.
> Specifically, don't use an unsigned type to represent a quantity just
> because it will never be negative (e.g."age" or "national debt").
> Use a signed type like int and you won't have to worry about boundary
> cases in the detailed rules for promoting mixed types.
> Only use unsigned types for bitfields or binary masks. Use casts in
> expressions, to make all the operands signed or unsigned, so the
> compiler does not have to choose the result type."
> 

So the author isn't talking about developing operating systems.  In
operating systems it's almost the other way around -- signed is really
the exception.

	-hpa
-- 
<hpa@transmeta.com> at work, <hpa@zytor.com> in private!
"Unix gives you enough rope to shoot yourself in the foot."
http://www.zytor.com/~hpa/puzzle.txt	<amsp@zytor.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-09-05  1:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-31  2:11 Advice on Unsigned Types John Kacur
2001-09-05  1:00 ` H. Peter Anvin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).