Main Content

MISRA C:2023 Rule 8.11

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

Since R2024a

Description

Rule Definition

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

Rationale

Although it is possible to declare an array with an incomplete type and access its elements, it is safer to state the size of the array explicitly. If you provide size information for each declaration, a code reviewer can check multiple declarations for their consistency. With size information, a static analysis tool can perform array bounds analysis without analyzing more than one unit.

Polyspace Implementation

The rule checker flags arrays declared with the extern specifier if the declaration does not explicitly specify the array size.

Troubleshooting

If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <stdint.h>

extern int32_t array1[10];    /*  Compliant  */
extern int32_t array2[];      /*  Non-compliant  */

In this example, two arrays are declared array1 and array2. array1 has external linkage (the extern keyword) and a size of 10. array2 also has external linkage, but no specified size. array2 is noncompliant because for arrays with external linkage, you must explicitly specify a size.

Check Information

Group: Declarations and Definitions
Category: Advisory
AGC Category: Advisory

Version History

Introduced in R2024a