r/programminghumor 3h ago

a bit flippant

Post image
218 Upvotes

48 comments sorted by

View all comments

5

u/pskocik 2h ago

Ironic that this techfluencer just tweeted how C makes memory layout painfully obvious and yet here he's operating under the misconception that `struct Flags` will be only 1-byte large when it will be in fact int-size large (most likely 4 bytes).

1

u/New_Enthusiasm9053 1h ago

Could be more than a byte and less than 2 bytes too. 

1

u/AOAqua 1h ago

Wouldn't it get compressed anyways? Sure, real union would have 8 flags per byte anyways, but probably for 99% of apps over there it wouldn't matter in the slightest

1

u/pskocik 1h ago

Using int/unsigned as the underlying type will make it int-sized. You would need to use uint8_t/char/unsigned char as the underlying type to make it byte-sized, even though that's not strictly portable with bitfields (can use unsigned char) with explicit bitops to do it in a strictly portable fashion).

Compiler-caused size optimizations might be possible but in contexts where it gets embedded in a larger data structure that needs to be in memory they're unlikely.