Data Structure Alignment

Data structure alignment is the way data is arranged and accessed in computer memory. It consists of two separate but related issues: data alignment and data structure padding. When a modern computer reads from or writes to a memory address, it will do this in word sized chunks (e.g. 4 byte chunks on a 32-bit system). Data alignment means putting the data at a memory offset 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, it may be necessary to insert some meaningless 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 4 bytes (a byte meaning 8 bits), the data to be read should be at a memory offset which is some multiple of 4. When this is not the case, e.g. the data starts at the 14th byte instead of the 16th byte, then the computer has to read two 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 ends at the 14th byte, the next data structure should start at the 16th byte. Two padding bytes are inserted between the two data structures to align the next data structure to the 16th byte.

Although data structure alignment is a fundamental issue for all modern computers, many computer languages and computer language implementations handle data alignment automatically. Ada, certain C and C++ implementations, D, and assembly language allow at least partial control of data structure padding, which may be useful in certain special circumstances.

Read more about Data Structure Alignment:  Definitions, Problems, Data Structure Padding, Typical Alignment of C Structs On X86, Allocating Memory Aligned To Cache Lines, Hardware Significance of Alignment Requirements

Famous quotes containing the words data and/or structure:

    Mental health data from the 1950’s on middle-aged women showed them to be a particularly distressed group, vulnerable to depression and feelings of uselessness. This isn’t surprising. If society tells you that your main role is to be attractive to men and you are getting crow’s feet, and to be a mother to children and yours are leaving home, no wonder you are distressed.
    Grace Baruch (20th century)

    Slumism is the pent-up anger of people living on the outside of affluence. Slumism is decay of structure and deterioration of the human spirit. Slumism is a virus which spreads through the body politic. As other “isms,” it breeds disorder and demagoguery and hate.
    Hubert H. Humphrey (1911–1978)