MISRA Discussion Forums

Full Version: 2 questions for rule 18.1
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The rule applies to structure definitions that are incomplete, but I see from exemplar suite file mc2_1801.c the following unexpected violation:

struct {
int8_t a;
int8_t b [ ]; /* Not Compliant - constraint error 6.5.2.1 */
} mc2_1801_st = { 1, { 2, 3, 4 } };

Are you including as "incomplete" any structure that has a flexible array member?

Apart from this, what other coding practice is left that might cause a problem?

If the type is incomplete, a compile error will result from any attempt to create an object of that type or refer to its members or to compute sizeof, etc.

So I cannot think of any legal C code that would be a violation of this rule.
Flexible array members are a feature of C99 and therefore not allowed according to the MISRA Standard (the current version of MSRA, 2004, is explicitly restricted to C89/C95). From a C89 point of view, the type is incomplete, and therefore in violation of 18.1.
If your compiler is silently (or by instruction) allowing certain/all C99 features to be used, your compiler will not consider the type itself incomplete, even if it is from the C89 viewpoint.

HTH,

Johan
The example cited was added to the exemplar suite because at least one commercial C90 compiler was found to accept flexible array members even though they are not part of the C90 language standard.

The exemplar suite for Rule 18.1 also includes an example of references to types that are incomplete which in turn lead to undefined behaviour.