Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MISRA C:2012 9.3 clarification
#1
I try to implement the checks for "9.3 - Arrays shall not be partially initialized" in Cppcheck, and would like to have some clarifications.

Is it ok to mix initializers like this, as long as every element gets initialized?

int32_t a[3] = { 1, 2, [2]=3 };

For multi-dimensional arrays, this would be compliant:

int32_t b[2][2] = { { 1, 2 }, { [1]=4 } };

beacuse b[1] is initialized with only a designated initializer.


As for c, both the array subobjects c[0] and c[1] are fully initialized, but there is no initialization of c[2]. Does that make the statement non-compliant?

int32_t c[3][2] = { { 1, 2 }, { 3, 4 } };


What about the arrays below, are they compliant or not, and why? Also, are they violatin 9.2 or 9.4?
int32_t d[1][2] = { [0][1]=2, [0]={1} };
int32_t e[1][2] = { [0]={1}, [0][1]=2 };
int32_t f[3][2] = { [1]={3, 4}, { 5, 6 }, [0][1]=2, [0][0]=1 };
int32_t g[2][2] = { { 1, 2 }, [1][0]=3, 4 };

Let me know what you think. If you have more (tricky) examples, then please share!
#2
Exception 2 only applies to array objects, not the suboject of an array.

Code:
int32_t a[3] = { 1, 2, [2]=3 };             // Compliant
int32_t b[2][2] = { { 1, 2 }, { [1]=4 } };  // Non-Compliant - exception 2 does not apply to subobjects
int32_t c[3][2] = { { 1, 2 }, { 3, 4 } };   // Non-Compliant c[2] must be explicitly represented as {0}
int32_t d[1][2] = { [0][1]=2, [0]={1} };    // Non-Compliant as [0]={1} does not fully initialise [0]
int32_t e[1][2] = { [0]={1}, [0][1]=2 };    // Non-Compliant as [0]={1} does not fully initialise [0]
int32_t f[3][2] = { [1]={3, 4}, { 5, 6 }, [0][1]=2, [0][0]=1 };  // Compliant
int32_t g[2][2] = { { 1, 2 }, [1][0]=3, 4 };  // Compliant with 9.4, but Non-Compliant with 9.2 (see example z3)
Posted by and on behalf of the MISRA C Working Group


Forum Jump:


Users browsing this thread: 2 Guest(s)