Concepts and theorems · Intermediate

Memory alignment: address-boundary requirements

Use four-byte alignment to distinguish address requirements, padding, and architecture-dependent unaligned access behavior.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Alignment is an address requirement
  2. Why a structure can contain padding
  3. Unaligned access is not one universal behavior
  4. Separate correctness from optimization
  5. Ask Logeia about this articleCheck your understanding

Alignment is an address requirement

An alignment requirement constrains the starting address of an object or memory operation. For four-byte alignment, the address must be divisible by four: 0x1000 qualifies, while 0x1001 does not. Object size and alignment are related but are not universally identical. The language, ABI, and particular instruction can impose different requirements. A portable explanation therefore names the relevant type or operation and its documented contract instead of assuming that every integer or every load has the same alignment on every machine.

Why a structure can contain padding

Imagine a structure containing a one-byte field followed by a field requiring four-byte alignment. Padding may be inserted between them so that the second field begins at a suitable address. Tail padding can also preserve alignment when several structures are placed in an array. As a result, adding member sizes need not produce the size of the whole structure. The exact layout is determined by the applicable implementation and ABI. Inspect size, alignment, and field offsets when layout matters rather than serializing a structure by assuming an illustrative layout is universal.

Unaligned access is not one universal behavior

Architectures differ in how they handle an unaligned access. Hardware may support it, support it with additional cost, or raise an exception for a particular operation. Even when an instruction can execute, forming and dereferencing an incorrectly aligned typed pointer can violate the programming-language contract. Parsing a byte buffer should use an allowed method, such as explicit byte assembly or a suitable copying operation, while also observing byte-order requirements. Processor tolerance is not sufficient evidence that an arbitrary source-language pointer cast is valid and portable.

Separate correctness from optimization

Meeting required alignment can be a correctness and portability condition, but increasing every alignment is not automatically a performance improvement. Extra padding consumes memory and can reduce the number of useful elements fitting in a cache. Conversely, some operations benefit from a particular boundary or avoid crossing a line when suitably aligned. Measure the actual workload and instruction requirements before optimizing layout. Packing a structure to remove padding also requires care: matching an external byte format does not eliminate the need to access its potentially unaligned fields through a safe supported method.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 4

Understand the systems behind the web · STEP 2

Prepare for AI systems · STEP 3

Choose a learning path