3 ms·
MSVC has `pragma pack` that removes padding to reduce memory usage, but I think it preserves field order: https://learn.microsoft.com/en-us/cpp/preprocessor/pa
by omoikane 3y ago
MSVC has `pragma pack` that removes padding to reduce memory usage, but I think it preserves field order:
https://learn.microsoft.com/en-us/cpp/preprocessor/pack?view=msvc-170 https://learn.microsoft.com/en-us/cpp/preprocessor/pack?view...
GCC and Clang also support this pragma:
https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Structure-Layout-Pragmas.html https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Structure-Layo...
https://clang.llvm.org/docs/UsersManual.html#microsoft-extensions https://clang.llvm.org/docs/UsersManual.html#microsoft-exten...
- PartiallyTyped 3y agoThat's right, you can pack things with a pragma directive in C and C++ for some compilers. Rust can take it a bit further. In rust, fields must be aligned to a multiple of their size, and structs/tuples must be aligned to a multiple of their largest element. This combination makes it very easy to handle memory offsets, or reusing the same representation in a way that reduces memory usage (these are called niches)[1]. This is why dynamic linking is a bit more complex in rust. Alternatively you can offer a C bridge via `extern "c"`. C will act as a bridge between rust and different languages, and at that stage you need to take care of ordering and padding. [1] https://doc.rust-lang.org/nomicon/repr-rust.html https://doc.rust-lang.org/nomicon/repr-rust.html