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 On the one hand, this struct’s layout is fully defined (on a per-platform basis). On the other hand, it worries me that you have a need to rely on this. On the third hand that I have grown for rhetorical purposes, https://en.cppreference.com/w/cpp/types/offsetof.html exists and even if you can’t use it wherever you actually need to know offsets, you can put it in a static_assert.
offsetof - cppreference.com

@jrose For some context:
- This particular project has a fixed, predictable set of target platforms and compilers it needs to work with.
- This particular project has automated tests, so if a platform we are running on produces surprising alignments, we'll know as soon as we run the automated test where I check that
- Although offsetof is a great suggestion, it doesn't help me here because I am doing C# FFI stuff and it is the C# code which needs to know the offsets

@jrose However, despite the automated tests, if someone had responded to my post with "uhh yo win32 has 3 bytes padding between each bool" it would have saved me a little bit of time.

someone else pointed there's no *downside* to just tossing #pragma pack in there, so i did.

@mcc @jrose

#pragma pack is fine, just make sure be aware of the alignment requirements. You can add unused char[] fields to pad it manually (if needed).
@viro_ssfs @mcc If you can explicitly pad to satisfy alignment, you don’t need pragma packed!