I have a program that uses dynamic allocation for a uint8_t array; can I safely assume that its length will always be one byte?
Asked
Active
Viewed 1.6k times
8
-
1Theoretically, no. You can't even assume `uint8_t` *exists*, even in a C99-compliant environment. Practically speaking, however, yeah it's pretty safe on anything even remotely modern. – nemequ Feb 07 '18 at 03:05
-
In practice, yes. I challenge you to find a modern architecture where this is not true. – Jonathon Reinhart Feb 07 '18 at 03:08
-
2@JonathonReinhart: It's not exactly modern, but some of the old-school Crays were "everything data type is 64 bits". I don't recall how they handled character data though, might have cheated a bit. They might have lasted long enough to develop C99 compilers. – ShadowRanger Feb 07 '18 at 03:11
-
5Why not use `sizeof (uint8_t)` and not worry about implementation details? – ad absurdum Feb 07 '18 at 03:12
-
1Related: https://stackoverflow.com/questions/2215445/are-there-machines-where-sizeofchar-1-or-at-least-char-bit-8 – Jonathon Reinhart Feb 07 '18 at 03:17
-
1@nemequ: If it exists it must be 1. – R.. GitHub STOP HELPING ICE Feb 07 '18 at 03:37
-
It seems a safe assumption, but why? In general, code is more robust when you minimize the number of assumptions. If you need the size of a uint8_t, why not just write sizeof(uint8_t)? – Dave Feb 07 '18 at 04:10
1 Answers
22
If uint8_t
exists, then sizeof(uint8_t)
must be 1. uint8_t
is required to have width exactly 8 and no padding bits, and CHAR_BIT
is required to be at least 8. If uint8_t
exists, it can't have more than one char's worth of bits without violating these requirements.

user2357112
- 260,549
- 28
- 431
- 505