From: dizzy on
Hi

I know that sizeof(char) == 1 (as per ISO C and C++), (if I remember well
from the C++ standard text) that the definition of a byte was the smallest
object that the machine could address, that a machine byte does hold a char
but we don't know yet that all possible values of a machine byte can be
stored in a char (or unsigned char for that matter).

3.9.1 1 doesn't seem to make it any more clear for "unsigned char".
For "char" it says there is no padding (object representation == value
representation) so I would say this means that indeed "char" is a byte for
the current platform (all byte values are char values and all char values
are byte values). However then for "unsigned char" it says that all bit
combinations of the value representation are valid numbers for it but this
does not tell me if it has padding or not.

To conclude, whenever some OS API that works with void* (because of
historical reasons) instead of char* but it says it writes/read bytes, I
can work with std::vector<char> buffers because I know a "char" is a byte
for all possible purposes but I cannot do that with "unsigned char" buffers
because from what I have been able to gather so far for "unsigned char"
there is no guarantee the value representation is the same as the object
representation (that there is no padding involved).

If it is so then I wonder how does one deal with treating the platform bytes
as pure unsigned integrals because once you read them into a "char" buffer
(which let's say it is signed on the current platform), some values may
appear as negative (if their bit representation matches negative number
representation of the platform char type) ?

Thanks!

--
Dizzy


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: clcppm-poster on
On 4 Jan, 19:41, dizzy <di...(a)roedu.net> wrote:
> Hi
>
> I know that sizeof(char) == 1 (as per ISO C and C++), (if I remember well
> from the C++ standard text) that the definition of a byte was the smallest
> object that the machine could address, that a machine byte does hold a
char
> but we don't know yet that all possible values of a machine byte can be
> stored in a char (or unsigned char for that matter).

I fail to see why an unsigned char could not be used to represent a
byte.

>From 5.3.3:
The sizeof operator yields the number of bytes in the object
representation of its operand. (...) sizeof(unsigned char) are 1.
Therefore an unsigned char always occupies one byte.

You can also verify this using std::numeric_limits<unsigned
char>::digits. This constant is equal to "The number of non-sign bits
in the representation" (18.2.1.2) which should be equal to the number
of bits per byte on your platform.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]