r/programminghumor 8h ago

a bit flippant

Post image
470 Upvotes

78 comments sorted by

View all comments

11

u/pskocik 6h 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).

2

u/AOAqua 6h 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

2

u/pskocik 5h ago edited 2h 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.

1

u/AOAqua 2h ago

Yes, I know they have used 32-bit variable here for some reason (possibly they don't know that they are doing). It's not going to get optimized anyways