News for hackers. Stuff that matters.
Re:Structures in C: From Basics to Memory Alignment by spacechild1 on Thursday June 29, 2023 @09:08PM (#36529236)
> the default ABI settings should get you completely compatible layouts here

That's not true! You must not assume that the alignment always equals the size of a type. For example, the SysV i386 ABI uses 32-bit alignment for 64-bit types (double, int64_t). The Microsoft x86 ABI, however, uses 64-bit alignment, as do all 64-bit ABIs (See https://stackoverflow.com/a/11110283.) If you want to share structs directly between different machines, you should use appropriate struct packing directives - unless you really know what you are doing.

Re:Structures in C: From Basics to Memory Alignment by LegionMammal978 on Friday June 30, 2023 @03:00AM (#36531465)
What's worse, MSVC's 32-bit x86 ABI reports an 8-byte alignment requirement (via __alignof) for 64-bit integer types, and its struct layout algorithm uses that alignment to determine padding, but those integers and structs are only aligned to 4 bytes when allocated on the stack! This has caused issues with Rust code trying to link with MSVC code [0], since Rust's standard library documentation asserts that properly aligned pointers have addresses that are always a multiple of the alignment used for struct layout.

[0] https://github.com/rust-lang/rust/issues/112480