intermediate · layout

Why Field Order Changes Struct Size

Reordering the fields of a struct changes its size without removing any field. Why?

Practical scenario

A struct with a bool, a pointer, an int and another bool is 32 bytes. Reordering the fields makes it 24. A reviewer asks why the compiler did not do that automatically.

What it tests

  • • Knowledge of alignment requirements and padding
  • • Whether the candidate can predict layout rather than only observe it
  • • Awareness that exact layout is ABI-specific

Lessons behind this question