4 ms·
> sizeof(uint16_t) == 1 > This is all completely legal according to the C standard. Is it? I don't have access to the standard, but from secondary sources[1]
by magicalhippo 4y ago
> sizeof(uint16_t) == 1
> This is all completely legal according to the C standard.
Is it? I don't have access to the standard, but from secondary sources[1] it seems not?
unsigned integer type with width of exactly 8, 16, 32 and 64 bits respectively
(provided if and only if the implementation directly supports the type)
If it is, it kinda defeats the whole purpose of uint16_t and friends.
[1]: https://en.cppreference.com/w/c/types/integer https://en.cppreference.com/w/c/types/integer
- dan-robertson 4y agoIt has 16 bits but the units of sizeof are not bytes – they are ‘the minimum difference between two addressable things’ which on that architecture is two bytes.
- tjoff 4y agoIsn't that a byte? So rather, a byte is 16 bits.
- actionfromafar 4y agoSure. A byte is two octets. In cases like these it pays to be very precise.
- rkangel 4y agoI can't remember the precise wording in the standard , but sizeof() is effectively "how many of the minimum addressable thing do you need to store this thing". The "minimum addressable thing" is called a byte, and on XAP that is a 16-bit value. Storing a uint8_t wastes a 8 bits, because there isn't anything smaller than an int to put it in.
- magicalhippo 4y agoRight, I was assuming the bytes were 8 bits[1] given sizeof(uint8_t) == 1, ie since it had uint8_t. Seems really weird to have uint8_t but have CHAR_BIT > 8. [1]: https://en.cppreference.com/w/c/language/sizeof https://en.cppreference.com/w/c/language/sizeof
- rkangel 4y agoThat's interesting. I didn't actually know the definition of uintXX_t. I was actually doing this with C90 most of the time, and using types we had defined (rather than from stdint.h). Sounds like they were more equivalent to int_leastXX_t. I realise know when I've previously written the above examples I've done it was basic types, so sizeof(char), sizeof(int) etc.. Sounds like it would be more correct as well!
- kps 4y agoYeah, checking C11 (FCD), “The typedef name uintN_t designates an unsigned integer type with width N and no padding bits.” My reading is that they should not have defined `uint8_t`. Probably they did so pragmatically (and maybe optionally) because so much code writes `intN_t` when they mean `int_leastN_t`, with 8 in particular. I don't think I've ever actually seen the `_least` or `_fast` versions used in real life. (I've worked with multiple processors with MAU>8; I recall at least one with 24-bit ‘bytes’, though I've forgotten what domain drove that.)
- magicalhippo 4y agoAh yes, having char and short etc be the same size makes a lot more sense, while still being a massive trap for young players.
- deleted 4y ago[deleted]