Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.INIT.BRACES

Incorrect initializer braces placement.

MISRA-C Rule 9.2 (required): Braces shall be used to indicate and match the structure in the non-zero initialisation of arrays and structures.

[Undefined 42]

ISO C requires initialiser lists for arrays, structures and union types to be enclosed in a single pair of braces (though the behaviour if this is not done is undefined). The rule given here goes further in requiring the use of additional braces to indicate nested structures. This forces the programmer to explicitly consider and demonstrate the order in which elements of complex data types are initialised (e.g. multi-dimensional arrays).

For example, below are two valid (in ISO C) ways of initialising the elements of a two dimensional array, but the first does not adhere to the rule:

int16_t y[3][2] = { 1, 2, 3, 4, 5, 6 };               /* not compliant */
int16_t y[3][2] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };   /* compliant     */

A similar principle applies to structures, nested combinations of structures, arrays and other types.

Note also that all the elements of arrays or structures can be initialised (to zero or NULL) by giving an explicit initialiser for the first element only. If this method of initialisation is chosen then the first element should be initialised to zero (or NULL), and nested braces need not be used.

The ISO standard [2] contains extensive examples of initialisation.

The intent of Rule 9.2 is that the non-zero initialisation of arrays and structures shall require an explicit initialiser for each element, e.g.

int16_t arraya1[5] = { 1, 2, 3, 0, 0 };
                  /* Compliant - non-zero initialisation              */
int16_t arraya2[5] = { 0 };
                  /* Compliant- zero initialisation                   */
int16_t arraya3[5] = { 1, 2, 3 };
                  /* Not Compliant - non-zero initialisation          */
int16_t arraya4[2][2] = { 0 };
                  /* Compliant - zero initialisation at top-level     */
int16_t arraya5[2][2] = { { 0 }, { 1, 2 }};
                  /* Not Compliant - zero initialisation at sub-level */

Zero or NULL initialisation shall only be applied at the top level of the array or structure.

MISRA-C++ Rule 8-5-2 (required): Braces shall be used to indicate and match the structure in the non-zero initialization of arrays and structures.

Rationale

ISO/IEC 14882:2003 [1] requires initializer lists for arrays, structures and union types to be enclosed in a single pair of braces (though the behaviour if this is not done is undefined). The rule given here goes further in requiring the use of additional braces to indicate nested structures. This forces the developer to explicitly consider and demonstrate the order in which elements of complex data types are initialized (e.g. multi-dimensional arrays).

The zero initialization of arrays or structures shall only be applied at the top level.

The non-zero initialization of arrays or structures requires an explicit initializer for each element.

Example

The following shows two valid ways of initializing the elements of a two dimensional array, but the first does not adhere to the rule:

int16_t y[3][2] = { 1, 2, 3, 4, 5, 6 };               // Non-compliant
int16_t y[3][2] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };   // Compliant

A similar principle applies to structures, and nested combinations of structures, arrays and other types.

Note also that all the elements of arrays or structures can be initialized (to zero or NULL) by giving an explicit initializer for the first element only. If this method of initialization is chosen then the first element should be initialized to zero (or NULL), and nested braces need not be used.

// The following are compliant
int16_t a1[5]    = { 1, 2, 3, 0, 0 };   // Non-zero initialization
int16_t a2[5]    = { 0 };               // Zero initialization
int16_t a3[2][2] = { };                 // Zero initialization
// The following are non-compliant
int16_t a4[5]    = { 1, 2, 3 };         // Partial initialization
int16_t a5[2][2] = { { }, { 1, 2 } };   // Zero initialization
                                        // at sub-level