Attribute pack_mode
The pragma {attribute 'pack_mode'}
defines the mode a data structure is packed while being allocated. Set the attribute on top of a data structure. It influences the packing of the whole structure.
{attribute 'pack_mode' := '<value>'}
The placeholder <value>
can have the following values:
|
Associated packing method |
---|---|
0 |
Aligned |
1 |
1-byte-aligned |
2 |
2-byte-aligned |
4 |
4-byte-aligned |
8 |
8-byte-aligned |
Depending on the structure, there may be no difference in the memory mapping of the individual modes. For example, the memory allocation of a structure with pack_mode = 4
can correspond to that of pack_mode = 8
.
If structures are combined to arrays, bytes are added at the end of each structure to achieve that the next structure is aligned.
'pack_mode'
is not used for a structure or corresponds to the alignment of the processor of the controller.
On some controllers, like the Modicon M262 Logic/Motion Controllers and Modicon M241 Logic Controllers, using a non natural alignment for structures may lead to an exception in the following cases:
When accessing elements of such structures that are unaligned (for example, a 4-byte variable located on an address not divisible by 4), with a POINTER variable.
When sharing such variables by using the
, for example, for accessing them from an OPC UA client.WARNING | |
---|---|
Furthermore, do not expose non-naturally aligned structures from libraries.
Do not use non-naturally aligned structures inside of visualization frames. Instead copy those structures into naturally aligned structures, and pass those to visualization frames.
{attribute 'pack_mode' := '1'}
TYPE myStruct:
STRUCT
Enable: BOOL;
Counter: INT;
MaxSize: BOOL;
MaxSizeReached: BOOL;
END_STRUCT
END_TYPE
A variable of data type myStruct
is instantiated aligned.
If the address of its component Enable
is 0x0100
, then the component Counter
will follow on address 0x0101
, MaxSize
on 0x0103
and MaxSizeReached
on 0x0104
.
With pack_mode=2
, Counter
would be found on 0x0102
, MaxSize
on 0x0104
and MaxSizeReached
on 0x0106
.