PLC

Alignment

An 8-byte alignment was introduced with TwinCAT 3.

System
Alignment
TwinCAT 2, x86
1 byte
TwinCAT 2, ARM
4 byte
TwinCAT 3
8 byte

Particular attention is required if data are exchanged as a whole memory array with other controllers or software components such as visualizations.

Memory location

A variable is stored at a location determined by its data type. Multiples of this value are possible as memory location. The maximum value depends on the system alignment.
A structure is stored at a location determined by the largest data type in the structure. Multiples of this value are possible as memory location.
The maximum value depends on the system alignment.

Memory space

From the above memory location a variable takes up as much memory as is determined by the size of its data type.
From the above memory location a structure takes up as much memory as is determined by the multiple of the largest data type in the structure. The maximum value depends on the system alignment, and the overall size is therefore limited to a multiple of it.

These alignment rules may result in implicit filler bytes.

Example (TwinCAT 3):

TYPE ST_Test1 :
STRUCT
    fVar   : LREAL;  // 64 bit
    nVar1  : DINT;   // 32 bit
    nVar2  : SINT;   // 8 bit
    (* 3 Füllbytes, um auf eine dem größten Datentyp entsprechende Grenze zu kommen (durch 8 byte teilbar) *)
END_STRUCT
END_TYPE
TYPE ST_Test2 :
STRUCT
    nVar2  : SINT;   // 8 bit
    (* 3 Füllbytes, um auf eine dem folgendem Datentyp entsprechende Grenze zu kommen (durch 4 byte teilbar) *)
    nVar1  : DINT;   // 32 bit
    fVar   : LREAL;  // 64 bit
END_STRUCT
END_TYPE
TYPE ST_Test3 :
STRUCT
    nVar2  : SINT;   // 8 bit
    (* 7 Füllbytes, um auf eine dem folgendem Datentyp entsprechende Grenze zu kommen (durch 8 byte teilbar) *)
    fVar   : LREAL;  // 64 bit 
    nVar1  : DINT;   // 32 bit
    (* 4 Füllbytes, um auf eine dem größten Datentyp entsprechende Grenze zu kommen (durch 8 byte teilbar) *)
END_STRUCT
END_TYPE

Due to the 8-byte alignment of TwinCAT 3, implicit padding bytes are added. The overall size of the structures can vary, although they contain three variables of the same data type.

On a system with 1-byte alignment the three structures would have the same memory requirement of 13 bytes.