Data alignment restriction means that many modern computer place restrictions on the allowable addresses for Primitive data types. They require that the addresses must be a multiple of some value $K$. This is used, because it simplifies the design of the Hardware.

An example of this is, that if a Central processing unit (CPU) always fetches 8 Byte from memory with an address that must be a multiple of 8. Then if a double abides this alignment restriction always only one access is necessary. But if it does not, two accesses are necessary to get the two parts of the double.

In the x86-64 architecture a Programm will work correctly even without data alignment. But it is recommended to use it to improve performance. For the following primitive types these multiple $K$ are used:

KTypes
1char
2short
4int, float
8long, double, char *

The alignment restriction is enforced by making sure that every data type is organized in such a way, that ever object within it satisfies the alignment restriction. This means that e.g. for Structure the Compiler must insert gaps of bytes.

For example for this structure:

struct S1 {
    int i;
    char c;
    int j;
};

One must use the offsets:

Additionally the end of a structure may need to be padded, so that if one declares an Array of structures the data alignment restriction still holds.

References

  • Computer Systems A Programmer's Perspective