Clive D.W. Feather
clive at demon.net
Tue Aug 3 09:09:05 UTC 2004
Paul Eggert said:
>>> + sizeof(short) <= sizeof(int) <= sizeof(long)
>> This one has gone. This does allow a perverse implementation where
>> long is 32 bits, all used, while short is 64 bits but only 17 of
>> them are used. There are reasons (too off-topic to go into) why we
>> did it this way.
> It's not entirely off-topic for tz, as a bit of it (difftime.c) does
> assume a relationship between sizeof and the range of values that can
> be stored.
Can you give me an example?
> I suspect there is a reasonable amount of code that makes
> the C89 sizeof assumptions, and which will silently go wrong of the
> assumptions fail to hold.
The only code I can think of that will have this problem is code that
assumes you can memcpy() a short into a long and have something sensible
> Is there some place that documents why these sizeof assumptions were
> removed in C99?
I don't recall what, if anything, we wrote in the Rationale.
The whole area of integer types and representations got revisited and
rewritten as part of the C99 process. In doing this, we identified the
properties that we thought were important:
* there's a hierarchy long long, long, int, short, char;
* integer types all come in signed/unsigned pairs;
* the range of lower types is a subset of the range of higher types;
* corresponding signed and unsigned types occupy the same storage;
* unsigned types can hold all possible non-negative values of the
corresponding signed type.
Note that while we removed the sizeof requirement, we *did* fix problems in
> Are there actual C99 implementations that violate
> these C89 assumptions?
I'm not aware either way.
Please note the following:
signed short ss = some_signed_value ();
signed long sl;
unsigned short us = some_unsigned_value ();
unsigned long ul;
sl = ss
if (sl != ss)
printf ("This can't happen in C90 or C99.\n");
ul = us
if (ul != us)
printf ("This can't happen in C99. This is legal C90.\n");
if (sizeof ul < sizeof us)
printf ("This is legal C99. It probably can't happen in C90.\n");
Clive D.W. Feather | Work: <clive at demon.net> | Tel: +44 20 8495 6138
Internet Expert | Home: <clive at davros.org> | Fax: +44 870 051 9937
Demon Internet | WWW: http://www.davros.org | Mobile: +44 7973 377646
Thus plc | |
More information about the tz