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
- A variable is located at the memory location that is specified by the size of its data type and by the alignment.
- If the data type is smaller than or equal to the alignment of the system, the memory location of the variable is equivalent to a multiple of the data type size.
- If the data type is larger than the alignment of the system, the memory location of the variable is equivalent to a multiple of the alignment.
- A structured data type (structure, function block) is located at the memory location that is specified by the size of the largest data type in the structure and by the alignment.
- If the largest data type is smaller than or equal to the alignment of the system, the memory location of the structure instance is equivalent to a multiple of this data type size.
- If the largest data type is larger than the alignment of the system, the memory location of the structure instance is equivalent to a multiple of the 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.
- A structured data type (structure, function block) takes up the amount of memory space, starting from the above memory location, as is specified by the size of the largest data type in the structure and by the alignment.
- If the largest data type is smaller than or equal to the alignment of the system, the memory space taken up by the structure instance is equivalent to a multiple of this data type size.
- If the largest data type is larger than the alignment of the system, the memory space taken up by the structure instance is equivalent to a multiple of the alignment.
These alignment rules may result in implicit padding bytes.
Please note (e.g. for a memory comparison with the help of the Tc2 system function MEMCMP), that padding bytes are not initialized. |
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.
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:
- The variable nLWORD is located in a memory location that is equivalent to a multiple of the alignment, since its data type (8 bytes) is larger than the alignment (4 bytes).
- The structured data type takes up an amount of memory space equivalent to a multiple of the alignment, since the largest data type in the structure (8 bytes) is larger than the alignment (4 bytes).
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:
- The variable nLWORD is located in a memory location that is equivalent to a multiple of the size of its data type, since its data type (8 bytes) is smaller than or equal to the alignment (8 bytes).
- The structured data type takes up an amount of memory space equivalent to a multiple of the size of the largest data type, since the largest data type in the structure (8 bytes) is smaller than or equal to the alignment (8 bytes).
Further information & sample project:
- Refer also to the Attribute 'pack_mode' in order to define how a data structure should be packed.
- On the PLC Samples page, you can also download a sample project on the topic of byte alignment.