I'm writing C++. I have a struct

struct ExampleStruct {
int32_t i;
bool a;
bool b;
}

I know I'm running on a modern CPU such as x86_64 or ARM64, and I know I'm using a modern compiler such as recent clang, recent gcc, or recent msvc.

The byte offsets of "a" and "b", relative to the start of ExampleStruct, are:

4 and 5
30.2%
4 and 8
16.3%
It depends on platform/compiler
44.1%
U gotta use a #pragma
9.4%
Poll ended at .
@mcc int is 4 bytes size, 4 bytes alignment, bools are 1 byte size, 1 byte alignment. the whole struct is 8 bytes size 4 bytes alignment. there's two bytes of padding at the end. mostly this is load bearing abi stuff so it shouldn't really change underneath you, even if it's not strictly specified by the C++ standard (it is specified by the platform ABI).
@mcc e.g. i believe sizeof(bool) can still be something stupid by the C++ law, in which case you might have a fun time. but if you find a platform where `sizeof(bool)` isn't 1 you can throw it into the fire anyway.
@dotstdy And "The ABI" in question is what? The Itanium ABI? A coincidental, silent agreement between the Itanium, ARM, and MS ABIs?
@mcc depends on the platform, idk which documents you need to look at to find the answers, but yes probably all three of those are important.

@mcc @dotstdy the x86 psABI and the ARM PCS define the size and alignment of the fields.

(Compiler implementations could chose to ignore them, but then they can’t interoperate with other binaries, so it’s fairly rare and confined to more niche types.)

@dotstdy @mcc even if it's probably safe to assume the padding won't change is there any cost to using #pragma pack
@bob @mcc yes, that will make it *less* portable, since it'll be misaligned.