Alignment

An 8-byte alignment was introduced with TwinCAT 3.

System

Alignment

TwinCAT 2, x86

1 byte

TwinCAT 2, ARM

4 bytes

TwinCAT 3

8 bytes

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

Memory location

Memory space

These alignment rules may result in implicit padding bytes.

Alignment 1:

Please note (e.g. for a memory comparison with the help of the Tc2 system function MEMCMP), that padding bytes are not initialized.

Alignment 2:

Largest data type in a function block

Each function block implicitly contains at least one pointer to store the address of the virtual method table. This means that in a target system with 64-bit architecture, the function block contains a data type with a size of 8 bytes. Depending on the function block, this can be the largest data type it contains, which determines the memory location and memory space.

Sample 1 (TwinCAT 3)

TYPE ST_Test1 :
STRUCT
    fVar   : LREAL;  // 8 Byte
    nVar1  : DINT;   // 4 Byte
    nVar2  : SINT;   // 1 Byte
    (* 3 filler bytes to reach a limit corresponding to the largest data type (divisible by 8 byte) *)
END_STRUCT
END_TYPE
TYPE ST_Test2 :
STRUCT
    nVar2  : SINT;   // 1 Byte
    (* 3 filler bytes to reach a limit corresponding to the following data type (divisible by 4 byte) *)
    nVar1  : DINT;   // 4 Byte
    fVar   : LREAL;  // 8 Byte
END_STRUCT
END_TYPE
TYPE ST_Test3 :
STRUCT
    nVar2  : SINT;   // 1 Byte
    (* 7 filler bytes to reach a limit corresponding to the following data type (divisible by 8 byte) *)
    fVar   : LREAL;  // 8 Byte 
    nVar1  : DINT;   // 4 Byte
    (* 4 filler bytes to reach a limit corresponding to the largest data type (divisible by 8 byte) *)
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.

Alignment 3:

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

Sample 2

TYPE ST_Test :
STRUCT
    nDWORD : DWORD;   // 4 Byte
    (* With an 8-byte alignment: 4 filler bytes *)
    nLWORD : LWORD;   // 8 Byte
END_STRUCT
END_TYPE

With a 4-byte alignment the structure has a size of 12 bytes. No padding bytes are inserted. Reason:

With an 8-byte alignment, conversely, the structure has a size of 16 bytes, since 4 padding bytes are inserted between the two variables. Reason:

Further information & sample project: