MISRA C:2012 Rule 6.3
Description
Rule Definition
A bit field shall not be declared as a member of a union1 .
This rule comes from MISRA C™: 2012 Amendment 3.
Rationale
The C standard does not specify the bitwise position of a bit field within a type.
For example, in this code, the bit field oneByte
can be the first
eight bits or the last eight bits of the int32_t
storage
unit.
int32_t oneByte:8;
oneByte
is overlaid on the 32-bit
number
:union myUnion { int32_t number; int oneByte: 8; }; void foo() { union myUnion U1; U1.number = 0xDEADBEEF; }
U1.oneByte
can be either
0xDE
or 0xEF
, depending on the
implementation.Using bit fields within unions makes the behavior of your code implementation-dependent. Avoid using bit fields within unions.
Polyspace Implementation
Polyspace® reports a violation of this rule if a bit field is declared as a member of
a union. Declaring a bit field as a subobject within union members is not a violation of
this rule. For example, if you have a bitfield within a struct
and
declare a union containing the struct
, this rule is not
violated.
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Types |
Category: Required |
AGC Category: Required |
Version History
Introduced in R2024a
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.