![]() |
|
MISRA-C:2004 Rule 3.5: (Required) |
|
||||
![]() | |||||||
1. To access the individual bits, or groups of bits, in larger data types (in conjunction with unions). This use is not permitted (see Rule ).
2. To allow flags or other short-length data to be packed to save storage space.
The packing together of short-length data to economise on storage is the only acceptable use of bit fields envisaged in this standard. Provided the elements of the structure are only ever accessed by their name, the programmer needs to make no assumptions about the way that the bit fields are stored within the structure.
It is recommended that structures be declared specifically to hold the sets of bit fields, and do not include any other data within the same structure. Note that Guideline need not be followed in defining bit-fields, since their lengths are specified in the structure.
If the compiler has a switch to force bit fields to follow a particular layout then this could assist in such a justification.
For example the following is acceptable:
If using bit fields, be aware of the potential pitfalls and areas of implementation-defined (i.e. non-portable) behaviour. In particular the programmer should be aware of the following:structmessage/* Struct is for bit-fields only */{signedintlittle:4;/* Note: use of basic types is required */unsignedintx_set:1;unsignedinty_set:1;}message_chunk;
* The alignment of the bit fields in the storage unit is implementation-defined, that is whether they are allocated from the high end or low end of the storage unit (usually a byte).
* Whether or not a bit field can overlap a storage unit boundary is also implementation-defined (e.g. if a 6-bit field and a 4-bit field are declared in that order, whether the 4 bit field will start a new byte or whether it will be 2 bits in one byte and 2 bits in the next).
No QAC messages cover this rule.
|
||
(c) The Motor Industry Research Association, 2004
|
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2 © 2013 Programming Research www.programmingresearch.com |
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index | Contents |