Data structure alignment: Difference between revisions

Content deleted Content added
Added detail about Data Structure Padding and revised Problems.
No edit summary
Line 1:
{{context}}
 
'''Data structureStructure alignmentAlignment''' is the way [[Data (computing)|data]] is arranged and accessed in [[computer memory]]. It consists of two separate but related issues: ''dataData alignmentAlignment'' and ''dataData structureStructure paddingPadding''. Data Alignment is the offset of a particular datum in computer memory from boundaries that depend on the datum type and processor characteristics. Data Structure Padding is the insertion of unnamed members in a data structure to preserve the relative alignment of the other structure members.

Although many computer languages and computer language implementations handle data alignment automatically, others allow at least partial control of data structure padding. In certain circumstances this might allow a program to use less memory for storing data in return for slower access to it. This compromise may be considered a form of [[space-time tradeoff]].
 
== Definitions ==
A memory address ''a'', is said to be ''n-byte aligned'' when ''n'' is a power of two and ''a'' is a multiple of ''n'' [[byte|bytes]]. In this context a byte is the smallest unit of memory access, i.e. each memory address specifies a different byte. An ''n''-byte aligned address would have ''log<sub>2</sub> n'' least-significant zeros when expressed in [[Binary numeral system|binary]].
 
A memory access is said to be ''aligned'' when the datum being accessed is ''n'' bytes long and the datum address is ''n''-byte aligned. When a memory access is not aligned, it is said to be ''misaligned''. Note that by definition byte memory accesses are always aligned.