Data structure alignment is the way data is arranged and accessed in computer memory. It consists of three separate but related issues: data alignment and data structure padding and packing. When a modern computer reads from or writes to a memory address, it will do this in word sized chunks, typically 32 bits or 64 bits on modern systems. Data alignment means putting the data at a memory address equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory. To align the data to the word boundaries, it may be necessary to insert some unused bytes between the end of the last data structure and the start of the next, which is data structure padding.
For example, when the computer's word size is 32 bits the data to be read should be aligned at a memory address which is some multiple of 4 bytes. When this is not the case, e.g. the data starts at address 14 instead of 16, then the computer has to read two or more 4 byte chunks and do some calculation before the requested data has been read, or it may generate an alignment fault. Even though the previous data structure end is at address 13, the next data structure should start at address 16. Two padding bytes are inserted between the two data structures at addresses 14 and 15 to align the next data structure at address 16.
When the data is smaller than the computer's word size, it may be possible to place several data elements in a single word. This is known as packing. For example, four 8-bit ASCII codes can be packed into a single 32-bit word, saving 96 bits compared to unpacked data. Using a packed structure requires the computer to process the word to re-construct the original data. This may slow performance on some machines, but the savings in memory may be worthwhile and can speed up overall performance in some architectures. The widespread use of packing on 6-bit character codes in the mainframe era led to machines with word lengths that were multiples of 6 bits; 36-bit machines were particularly common. The introduction of ASCII led to new designs with word lengths that are multiples of 8 bits, 7 bits of code and a parity bit.
Although data structure alignment is a fundamental issue for all modern computers, many computer languages and computer language implementations handle data alignment automatically. Ada,PL/I,Pascal, certain C and C++ implementations, D,Rust, and assembly language allow at least partial control of data structure padding, which may be useful in certain special circumstances.