This rule is part of MISRA C++:2023.
Usage of this content is governed by Sonar’s terms and conditions. Redistribution is
prohibited.
Rule 6.0.2 - When an array with external linkage is declared, its size should be explicitly specified
Category: Advisory
Analysis: Decidable,Single Translation Unit
Amplification
This rule applies to non-defining declarations [1] only. It is possible to define an array and specify its size implicitly by means of
initialization.
Rationale
Although it is possible to declare an array with incomplete type and access its elements, it is safer to do so when the size of the array may be
explicitly determined. Providing size information for each declaration [1] permits them to be checked for consistency. It may also permit a
static checker to perform some array bounds analysis without needing to analyse more than one translation unit.
Example
int32_t array1[ 10 ]; // Compliant
extern int32_t array2[ ]; // Non-compliant
int32_t array3[ ] = { 0, 10, 15 }; // Compliant
extern int32_t array4[ 42 ]; // Compliant
Glossary
[1] Declaration
A declaration introduces the name of an entity into a translation unit (see [basic.def]/1).
An entity may be declared several times. The first declaration of an entity in a translation unit is
called an introduction [2]. All subsequent declarations are called redeclarations [3].
A definition [4] is a declaration, as described in [basic.def]/2.
[2] Introduction
See declaration [1].
[3] Redeclaration
See declaration [1].
[4] Definition
See declaration [1].
Copyright The MISRA Consortium Limited © 2023