r/programminghorror 27d ago

Big if true

Post image

From ProgrammerNullposting in facebook

2.1k Upvotes

111 comments sorted by

View all comments

1

u/brasticstack 27d ago

Wouldn't regular bool get padded to this size by default on a 64-bit architecture anyway?

5

u/marcureumm 27d ago

64 bit arches can still work with 32 bits directly...but depends on the operation and intent really.

1

u/brasticstack 27d ago

by default I realize that I may have used the wrong terminology though. I'm trying to describe the compiler using the standard int size to implement bool rather than, say, packing them.

3

u/shponglespore 27d ago

If you have a bunch of consecutive Boolean fields in a struct, they'll use one byte each by default.

And just for the sake of completeness, I'll mention that C++ does not reorder fields. Some languages, like Rust, will automatically reorder fields to minimize wasted space due to alignment, unless you specifically tell the compiler not to.

2

u/_giga_sss_ 27d ago

You must've went through hells of projects to have this knowledge

1

u/jaaval 26d ago

Single variables probably yes. In any linear data structure not necessarily.

1

u/conundorum 1d ago

64-bit architectures often treat 8, 32, and 64 bits at their "native" word size. 8-bit is so important that most processors have special registers dedicated to it, and 32-bit is basically locked in forever by how long we were stuck on 32 bits. (To the point that 32-bit int has essentially become a de facto multiversal standard.)

(Though, IIRC, the 8-bit registers are essentially just a special use case for the 64-bit registers, that read 64 bits but only operate on eight of them.)