Content deleted Content added
provide context in introduction |
Johnmorgan (talk | contribs) Added detail about Data Structure Padding and revised Problems. |
||
Line 1:
{{context}}
'''Data structure alignment''' is the way [[Data (computing)|data]] is arranged and accessed in [[computer memory]].
== Definitions ==
Line 13:
== Problems ==
A computer accesses memory a single memory word at a time.
If the highest and lowest bytes in a datum are not within the same memory word the computer must split the datum access into multiple memory accesses. This requires a lot of complex circuitry to generate the memory accesses and coordinate them. To handle the case where the memory words are in different memory
When a single memory word is accessed the operation is atomic, i.e. the whole memory word is read or written at once and other devices must wait until the read or write operation completes before they can access it. This may not be true for unaligned accesses to multiple memory words, e.g. the first word might be read by one device, both words written by another device and then the second word read by the first device so that the value read is neither the original value nor the updated value. Although such failures are rare, they can be very difficult to identify.
==Data Structure Padding==
Although the language translator (compiler or interpreter) normally allocates individual data items on aligned boundaries, data structures often have members with different alignment requirements. To maintain proper alignment the translator normally inserts additional unnamed data members so that each member is properly aligned. In addition the data structure as a whole may be padded with a final unnamed member. This allows each member of an entire array of structures to be properly aligned.
Members of a data structure can be arranged to minimize the amount of padding. For example, members may be sorted into ascending or descending alignment requirements. Although C and C++ do not allow the compiler to do such reordering, other languages might. It is also possible to tell most C and C++ compilers to "pack" the members of a structure to a certain level of alignment, e.g. "pack(2)" means align data members larger than a byte to a two-byte boundary so that any padding members are at most one byte long.
Although use of "packed" structures is most frequently used to conserve memory space, it may also be used to format a data structure for transmission using a standard protocol. Since this depends upon the native byte ordering ([[endinanness]]) for the processor matching the byte ordering of the protocol, this usage is not recommended.
==Unaligned Pointer Support==
|