Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The C standard Standard allows an array variable to be declared both with a bound and with an initialization literal. The initialization literal also implies an array bound in the number of elements specified.

...

but it is also possible to use designators to initialize array elements in a noncontiguous fashion. Section 6.7.9, Example 12, of the C standard Standard [ISO/IEC 9899:2011] states:

Space can be "allocated" from both ends of an array by using a single designator:

Code Block
int a[MAX] = {
  1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0
};

In the above, if MAX is greater than ten, there will be some zero-valued elements in the middle; if it is less than ten, some of the values provided by the first five initializers will be overridden by the second five.

The C standard also Standard also dictates how array initialization is handled when the number of initialization elements does not equal the explicit array bound. Section 6.7.9, para. paragraphs 21–22, states:

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

...

Tool

Version

Checker

Description

Compass/ROSE

 

 

 

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

araydecl

Fully implemented

LDRA tool suite

Include Page
LDRA_V
LDRA_V

127 S
397 S
404 S

Partially implemented

ECLAIR

Include Page
ECLAIR_VECLAIR_V

araydecl

Fully implemented

PRQA QA-C
Include Page
PRQA_V
PRQA_V

0684 (C)
0686
0687
0688
3674
3684

Fully implemented

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

...

Bibliography

...

]Section 6.7.9, "Initialization

...

"

...

...