How does struct field ordering affect memory layout in Go and Rust?
It tests alignment, padding, and compiler layout knowledge. A strong answer explains that alignment inserts padding, Go and Rust keep declared order, and reordering by size can shrink size. Red flag: saying order is irrelevant or that compiler auto-packs.
WHAT THIS TESTS: This question probes whether you understand memory alignment, data structure padding, and packing at the ABI level, not just language syntax. Interviewers want to see that you know how the compiler translates struct declarations into bytes, why field order matters, and that you can reason about cache lines and memory density.
A GOOD ANSWER COVERS: First, define alignment as the requirement that a data type be stored at a memory address that is a multiple of its size. Second, explain that compilers insert padding bytes between struct fields to satisfy alignment constraints for each member and for the overall struct, which must align to its most restrictive member. Third, state explicitly that Go and Rust compilers preserve the source order of fields and do not automatically reorder them to minimize padding, unlike some other languages or specific optimization passes. Fourth, describe how reordering fields from largest to smallest can eliminate internal padding and reduce total size. Fifth, mention that the size of the struct is rounded up to the nearest multiple of its alignment.
COMMON WRONG ANSWERS: Claiming that Go or Rust automatically reorder fields to optimize memory layout is a major red flag. Another mistake is confusing struct size with the sum of field sizes without accounting for padding. Some candidates also incorrectly state that alignment only matters for performance and never for correctness, or they give an example that does not actually change size because they mixed up the ordering or architecture. Saying you can ignore alignment on modern hardware is also incorrect.
LIKELY FOLLOW-UPS: The interviewer may ask how you would force a specific layout, leading to discussion of packed structs in Rust or unsafe tricks in Go, and the trade-offs with unaligned access. They might ask about the impact on cache lines, false sharing, or how slice or array layouts amplify wasted space. Another follow-up is comparing struct layout across architectures, such as 32-bit versus 64-bit, or asking about the size of empty structs and zero-width types.
ONE CONCRETE EXAMPLE: Consider a 64-bit architecture where an int64 has 8-byte alignment and a bool has 1-byte alignment. In Go, struct A with fields bool, int64, bool uses 24 bytes: the first bool at offset 0, then 7 bytes padding, the int64 at offset 8, the second bool at offset 16, then 7 bytes trailing padding to make the total size a multiple of 8. If you reorder to int64, bool, bool, the size drops to 16 bytes: int64 at offset 0, first bool at offset 8, second bool at offset 9, and 7 bytes trailing padding to reach a multiple of 8. The same logic applies in Rust with i64 and bool.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.