SA0161: Unpacked structure in packed structurePermalink

Detects unpacked structures that are used in packed structures

Justification: The compiler typically sets an unpacked structure to an address that allows 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

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

--> SA0161: Declaration of an unpacked struct 'struct9ByteData' inside a packed struct 'structSingleDataRecord'
--> SA0161: Declaration of an unpacked struct 'struct4ByteData' inside a packed struct 'structSingleDataRecord'