Implicit assumptions (was: Re: Some fun with -O2)
Mark Millard
marklmi at yahoo.com
Fri Jan 15 18:35:50 UTC 2021
On 2021-Jan-15, at 10:11, Walter von Entferndt <walter.von.entferndt at posteo.net> wrote:
> At Freitag, 15. Januar 2021, 08:02:49 CET Mark Millard wrote:
>> FYI: C itself has, in <limits.h> , CHAR_BIT for the number of bits in a
>> Byte for how sizeof counts Bytes: sizeof(char), sizeof(signed char),
>> and sizeof(unsigned char) are each always 1.
>>
> No, CHAR_BIT is the #bits in a *char*, which is the (standard) datatype for
> the binary representation of a character/letter/symbol in written human
> language, and for small integers. The name also suggests that, as well as the
> comment in the header file. That does not necessarily equal a "byte", which
> (by commonly accepted knowledge) is the smallest addressable entity in a
> computer's memory. Of course, e.g. https://code-reference.com/c/datatypes/
> char tells a *char* occupies one byte. Sadly, AFAIK C itself does not define
> what a "byte" is, although that term is mentioned many times in reference
> manuals (implicit assumption). So /theoretically/ CHAR_BIT and NBBY can
> differ. In fact, many library funtions operating on characters/letters take
> an *int* instead of a *char* for performance reasons. From https://code-reference.com/c/stdlib.h/sizeof: "the *sizeof* operator returns the number of
> bytes to be reserved for a variable or a data type". Of course, for practical
> reasons, we can safely assume that a *char* will take one byte in storage
> space for the foreseeable future, since the consequences of changing that
> would be disastrous.
Have you read a (fairly modern) C standard or its officially
published rationle? You might want to.
From the officially published C99 rationale (page labeled 11,
Terms and definitions):
QUOTE
) All objects in C must be representable as a contiguous sequence of bytes,
each of which is at least 8 bits wide.
) A char whether signed or unsigned, occupies exactly one byte.
(Thus, for instance, on a machine with 36-bit words, a byte can be defined
to consist of 9, 12, 18, or 36 bits, these numbers being all the exact
divisors of 36 which are not less than 8.) These strictures codify the
widespread presumption that any object can be treated an an array of
characters, the size of which is given by the sizeof operator with that
object's type as its operand.
END QUOTE
===
Mark Millard
marklmi at yahoo.com
( dsl-only.net went
away in early 2018-Mar)
More information about the freebsd-hackers
mailing list