Unit content
Data layout, alignment and padding
A program's logical record layout becomes a concrete arrangement of bytes in memory. Hardware and language alignment rules can insert unused padding between fields or at the end of a record.
Alignment
Many machine operations prefer or require values to begin at addresses divisible by a particular alignment. A four- or eight-byte value is therefore commonly placed on a corresponding boundary.
Padding
Consider fields whose sizes differ. Reordering them can change the amount of padding while preserving the same logical information.
small | padding | large | small | padding
versus a layout that groups larger-alignment fields together.
Size affects locality
A smaller record can let more useful records fit in each cache line. Conversely, storing fields that a hot loop never reads can waste memory bandwidth even if the record is perfectly aligned.
Layout is part of performance, not meaning
Field order and representation should first preserve correctness and a maintainable model. When measurements show memory traffic matters, layout becomes an optimization dimension involving footprint, alignment, vectorization and access patterns.
Understanding padding explains why two source-level structures containing the same logical fields need not occupy the same number of bytes.