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.PARTIAL.2012

Arrays shall not be partially initialized.

MISRA C 2012 Rule 9.3: Arrays shall not be partially initialized

Category: Required

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Amplification

If any element of an array object or subobject is explicitly initialized, then the entire object or subobject shall be explicitly initialized.

Rationale

Providing an explicit initialization for each element of an array makes it clear that every element has been considered.

Exception

  1. An initializer of the form { 0 } may be used to explicitly initialize all elements of an array object or subobject.
  2. An array whose initializer consists only of designated initializers may be used, for example to perform a sparse initialization.
  3. An array initialized using a string literal does not need an initializer for every element.

Example

/* Compliant */ 
int32_t x[ 3 ] = { 0, 1, 2 };

/* Non-compliant - y[ 2 ] is implicitly initialized */ 
int32_t y[ 3 ] = { 0, 1 };

/* Non-compliant - t[ 0 ] and t[ 3 ] are implicitly initialized */ 
float32_t t[ 4 ] = { [ 1 ] = 1.0f, 2.0f };

/* Compliant - designated initializers for sparse matrix */ 
float32_t z[ 50 ] = { [ 1 ] =  1.0f, [ 25 ] = 2.0f };

In the following compliant example, each element of the array arr is initialized:

float32_t arr[ 3 ][ 2 ] = 
{
  { 0.0f, 0.0f }, 
  { PI / 4.0f, -PI / 4.0f }, 
  { 0 } /* initializes all elements of array subobject arr[ 2 ] */ 
};

In the following example, array elements 6 to 9 are implicitly initialized to '\0':

char h[ 10 ] = "Hello"; /* Compliant by Exception 3 */