Main Content

AUTOSAR C++14 Rule A3-1-4

When an array with external linkage is declared, its size shall be stated explicitly

Description

Rule Definition

When an array with external linkage is declared, its size shall be stated explicitly.

Rationale

Declaring an array with external linkage without specifying its size and then attempting to access elements of the array can result in an array index out of bounds error. To reduce the possibility of such errors, specify the size of an array in its declaration.

Polyspace Implementation

Polyspace® reports a violation of this rule if all of these conditions are true:

  • You declare an extern array.

  • The array is not initialized.

  • The number of elements for the array is not explicitly stated.

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]; //Compliant

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

Check Information

Group: Basic Concepts
Category: Required, Automated

Version History

Introduced in R2019a