Main Content

MISRA C++:2023 Rule 6.0.2

When an array with external linkage is declared, its size should be explicitly specified

Since R2024b

Description

Rule Definition

When an array with external linkage is declared, its size should be explicitly specified.

Rationale

Though you can declare an incomplete array type and later complete the type, specifying the array size during the first declaration makes the subsequent array access less error-prone.

Polyspace Implementation

Polyspace® reports a violation of this rule if you declare an external array without specifying its size.

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

int array[10];  
extern int array2[]; //Noncompliant
int array3[]= {0,1,2};
extern int array4[10];

In the declaration of array2, the array size is unspecified.

Check Information

Group: Basic Concepts
Category: Advisory

Version History

Introduced in R2024b