Main Content

MISRA C++:2008 Rule 8-5-2

Braces shall be used to indicate and match the structure in the non- zero initialization of arrays and structures

Description

Rule Definition

Braces shall be used to indicate and match the structure in the non- zero initialization of arrays and structures.1

Rationale

The use of nested curly braces in initializer lists to match the structures of nested objects in arrays, unions, and struct objects encourages you to consider the order of initialization of complex data types and makes your code more readable. For example, the use of curly nested braces in the initialization of ex1 makes it easier to see how the nested arrays arr1 and arr2 in ex1 are initialized.

struct Example
{
    int num;
    int arr1[2];
    int arr2[3];
};

//....
struct Example ex1 {1, {2, 3}, {4, 5, 6}}; //Compliant

The rule does not require the use of nested curly braces if you zero initialize an array, a union, or a struct object with nested structures are the top-level, for instance:

struct Example ex1 {}; //Compliant

Polyspace Implementation

If you non-zero initialize an array, union, or struct object that contains nested structures and you do not use nested curly braces to reflect the nested structure, Polyspace® reports a violation on the first element of the first nested structure in the initializer list. For instance, in this code, Polyspace reports a violation on the number 2 because it corresponds to the first element of nested structure arr1 inside ex1.

struct Example
{
    int num;
    int arr1[2];
    int arr2[3];
};

//....
struct Example ex1 {1, 2, 3, 4, 5, 6}; // Noncompliant

Polyspace does not report violations of this rule on containers implemented by the Standard Template Library (STL).

If you zero-initialize an array or an structure, initialize only the first element to zero or NULL. Partially initializing an array or a struct object to non-zero value and relying on implicit zero initialization for the rest of the object results in a violation. For example, in this code, the partial initializations are reported as violations:

int16_t array[5] = {1,2,3}; //Noncompliant
int8_t map[2][2] = {{},{1,1}}; //Noncompliant

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

char arr1[2][3] {'a', 'b', 'c', 'd', 'e', 'f'}; //Noncompliant
char arr2[2][3] {{'a', 'b', 'c'}, {'d', 'e', 'f'}}; //Compliant
char arr_top_level[2][3] { }; //Compliant
char arr_sub_level[2][3] { {}, {'d', 'e', 'f'}}; //Noncompliant

In this example, the initializer list of the two-dimensional array arr1 violates the rule because it does not reflect the nested structure of the array (two arrays of three elements each). The initialization of arr2 uses nested curly braces to reflect the nested structure of the array and is compliant. Similarly, the initialization of arr_top_level is compliant because it zero initializes the array at the top level of the nested structure. The initialization of arr_sub_level violates this rule because it zero-initializes only the first sub-array while explicitly initializing all the elements of the second sub-array.

Check Information

Group: Declarators
Category: Required

Version History

Introduced in R2013b

expand all


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.