Monday, March 22, 2010

Do not mix bit-fields




Do not mix bit-fields other data within the same structure (misra2004_3_5_BitFieldStructuresWithoutOtherData.rule)


Description

It is recommended that structures should be declared specifically to hold the sets of bit fields, and do not include any other data within the same structure.

Benefits:

Rule prevents from the potential pitfalls and areas of implementation-defined (i.e.non-portable) behaviour.

Example:

struct message {  /* Violation */
   signed int little: 4;
   unsigned int x_set: 1;

   int size;
};

Repair:

struct message {  /* OK */
   signed int little: 4;
   unsigned int x_set: 1;
};

References:

MISRA-C:2004 Guidelines for the use of the C language in critical systems

Chapter 6, Section 3

Author
ParaSoft


No comments:

Post a Comment

Labels