Ian Collins said:
Malcolm McLean wrote: [...]
Whilst there is an argument for having fixed-size types in a
language, C chose the other route. We are now seeing a reversal of
policy so that existing 32-bit code won't break under 64 bits. That is,
badly-written 32-bit code. If it was written properly it wouldn't assume
32 bit ints and longs.
So you are labelling the BSD networking code as bad? The assumption
that long is 32 bits is pretty widespread there.
Is it? If so, I'd say that's a bad assumption. I'd be very surprised
if that code isn't already running on systems with 64-bit longs.
When ever C hits the real world, developers have added fixed size types.
Now they are standardised. Good.
Agreed. Though I suspect fixed size types tend to be overused. In
many cases, what you really need is a type able to represent (at
least) some specified range of values. C99's <stdint.h>,
unfortunately IMHO, gave the easy-to-remember names to the exact-width
types rather than the potentially more useful least-width types.
For example:
int_least32_t is the narrowest signed type of at least 32 bits.
int_fast32_t is the "fastest" signed type of at least 32 bits.
int32_t is *exactly* 32 bits wide; if there is no such type,
int32_t doesn't exist.
On a 64-bit CPU, for example, int_least32_t might be 32 bits and
int_fast32_t might be 64 bits.
I think that int32_t, the exact-width type, is really needed only for
conformance to some externally imposed data layout. For internal
numeric calculations, int_fast32_t or int_least32_t is more
appropriate, depending on whether you need large arrays. But since
the name "int32_t" is more obvious, a lot of programmers are going to
use it unnecessarily.
That's probably not *too* bad. If int32_t exists, it's going to be
the same as int_least32_t, and if it doesn't, the code will fail to
compile in a rather obvious manner. (But then the programmer may be
tempted to define his own "int32_t" that's actually 64 bits.)
IMHO, it would have been better to define:
int_least32_t
int_fast32_t
int_exact32_t
or perhaps even to introduce a new syntax for declaring such types
rather than using ad hoc typedefs.