Actions

::Data structure alignment

::concepts



{{ safesubst:#invoke:Unsubst||$N=Refimprove |date=__DATE__ |$B= {{#invoke:Message box|ambox}} }} 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) or larger. 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, 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 means 8 bits on most machines, but could be different on some systems), the data to be read should be at a memory address which is some multiple of 4. 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.

Although data structure alignment is a fundamental issue for all modern computers, many computer languages and computer language implementations handle data alignment automatically. Ada,<ref>{{#invoke:citation/CS1|citation |CitationClass=book }}</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=book }}</ref> PL/I, certain C and C++ implementations, D,<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> and assembly language allow at least partial control of data structure padding, which may be useful in certain special circumstances.


Data structure alignment sections
Intro  Definitions  Problems  Architectures  Data structure padding  Typical alignment of C structs on x86  Allocating memory aligned to cache lines  Hardware significance of alignment requirements  See also  References  Further reading  External links  

PREVIOUS: IntroNEXT: Definitions
<<>>

Aligned::memory    Bytes::address    -byte::source    Align::padding    Members::would    Offset::access

{{ safesubst:#invoke:Unsubst||$N=Refimprove |date=__DATE__ |$B= {{#invoke:Message box|ambox}} }} 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) or larger. 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, 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 means 8 bits on most machines, but could be different on some systems), the data to be read should be at a memory address which is some multiple of 4. 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.

Although data structure alignment is a fundamental issue for all modern computers, many computer languages and computer language implementations handle data alignment automatically. Ada,<ref>{{#invoke:citation/CS1|citation |CitationClass=book }}</ref><ref>{{#invoke:citation/CS1|citation |CitationClass=book }}</ref> PL/I, certain C and C++ implementations, D,<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref> and assembly language allow at least partial control of data structure padding, which may be useful in certain special circumstances.


Data structure alignment sections
Intro  Definitions  Problems  Architectures  Data structure padding  Typical alignment of C structs on x86  Allocating memory aligned to cache lines  Hardware significance of alignment requirements  See also  References  Further reading  External links  

PREVIOUS: IntroNEXT: Definitions
<<>>