Overview

For a large class of modern ISAs, storage for basic C datatypes respect self-alignment. This means chars can start on any byte address, shorts on any even address, 4-byte ints and floats must start on an address divisible by 4, and doubles must start on an address divisible by 8. Likewise pointers are also self-aligned.

Wasted space introduced solely for alignment purposes is referred to as slop.

Structures

A struct’s stride address refers to the first address following the struct data that has the same alignment as the struct. In general the compiler adds various constraints to how a struct is layed out:

  1. The struct’s alignment follows that of its widest scalar member.
    • This guarantees each member satisfies its own self-alignment requirement.
  2. The struct introduces trailing padding up to its stride address.
    1. This ensures each element in an array satsifies its self-alignment requirement.

The sizeof operator on a struct returns the total space used between its leading address and its stride address.

Unions

The sizeof operator on a union returns that of its widest member.

Bibliography

  • Bryant, Randal E., and David O’Hallaron. Computer Systems: A Programmer’s Perspective. Third edition, Global edition. Always Learning. Pearson, 2016.
  • Raymond, Eric. “The Lost Art of Structure Packing.” Accessed November 4, 2024. http://www.catb.org/esr/structure-packing/.