Automatic Data Alignment

Automatic Data Alignment

The data alignment in computer memory is a way to store data in memory in a special manner to speed up access to the information. The data alignment is an issue, related to the equipment. It means putting the data in the memory address, which increases the system’s performance in order to structure of the memory of the central processing unit. To align the data, it may be necessary to insert some meaningless bytes between the end of the last data structure and to start of the next.

Modern processors are designed in such a way that they read the data in separate pieces of 32 bits. In addition, these pieces are always aligned at the 32-bit boundary. This means that the memory addresses transferred from the processor to the cache memory are always divided into four without a remainder (4 bytes = 32 bits), so the two least significant bits of the address are zero. When 64-bit processors become quite common, addressing will turn into 64-bit (or 128-bit) and the alignment will be performed already on the new boundary.

What is the relationship between the data alignment and applications? When programming, you need to make sure that variables and pointers are aligned on a four-byte or 32-bit boundary. If they pass through the boundary 4 bytes, the processor will have to issue two instructions for reading the cache: the first command to read the first part, and the second to the second part. Then the processor will need to connect the two parts of the value and discard the unnecessary bits. In a number of processors, 32-bit values must always be aligned at the 32-bit boundary, otherwise, an access violation occurs. Fortunately, Intel processors do not require this, which in turn provokes programmers to some negligence.

Always make sure that 32-bit values are aligned at the 32-bit boundary, and 16-bit values are aligned at the 16-bit boundary. To increase the performance, you need to make sure in the 64-bit values. For example, double variables are aligned on a 64-bit boundary. All this sounds difficult enough, but in reality, the programmer is very much helped by the Delphi compiler. As a result, special attention should be paid only to the declaration of type record. All global and local atomic variables are aligned properly. If the alignment type is not set, the 32-bit Delphi compiler will automatically align and record type fields. For this, it adds insignificant bytes. In the 16-bit version, the automatic alignment of variable atomic types is not used.

Notwithstanding the data structure alignment is a fundamental characteristic of modern computer systems, many computer languages produce a data alignment automatically. They enable at least partial control of the data structure lining, which may be useful in certain special circumstances.