public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: John Kacur <jkacur@home.com>
To: linux-kernel@vger.kernel.org
Subject: Advice on Unsigned Types
Date: Thu, 30 Aug 2001 22:11:53 -0400	[thread overview]
Message-ID: <3B8EF269.BF457C7F@home.com> (raw)

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

             reply	other threads:[~2001-08-31  2:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-31  2:11 John Kacur [this message]
2001-09-05  1:00 ` Advice on Unsigned Types H. Peter Anvin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3B8EF269.BF457C7F@home.com \
    --to=jkacur@home.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox