Skip to main content

SA0161: Unpacked structure in packed structure

Detects unpacked structures which are used in packed structures

Justification: The compiler typically sets an unpacked structure to an address which allows an aligned access to all elements within the structure. If you create this structure in a packed structure, then aligned access is no longer possible. Furthermore, access to an element in the unpacked structure can lead to a "misalignment exception".

Importance: High

Example 119. Example

The structure structSingleDataRecord is packed, but it contains the unpacked structures struct4Byte and struct9Byte.

{attribute 'pack_mode' := '1'}
TYPE structSingleDataRecord :
STRUCT
    str9ByteData: struct9Byte;    (* 9 BYTE *)
    str4ByteData: struct4Byte;    (* 4 BYTE *)
    udi1: UDINT;
    udi2: UDINT;
    udi3: UDINT;
    usi4: USINT;
END_STRUCT
END_TYPE
(* 9 BYTE *)
TYPE struct9Byte :
STRUCT
    usiRotorSlots: USINT;    (* 1 BYTE *)
    uiMaxCurrent: UINT;    (* 2 BYTE  *)
    usiVelocity: USINT;    (* 1 BYTE  *)
    uiAcceleration: UINT;    (* 2 BYTE  *)
    uiDeceleration: UINT;    (* 2 BYTE *)
    usiDirectionChange: USINT;    (* 1 BYTE *)
END_STRUCT
END_TYPE
TYPE struct4Byte :
STRUCT
    //udiDummy : UDINT;
    rRealDummy : REAL;
END_STRUCT
END_TYPE

Output in the Messages view:

  • sa_icon_message.png SA0161: Declaration of an unpacked struct 'struct9ByteData' inside a packed struct 'structSingleDataRecord'

  • sa_icon_message.png SA0161: Declaration of an unpacked struct 'struct4ByteData' inside a packed struct 'structSingleDataRecord'