Main Content

MISRA C:2012 Rule 18.8

Variable-length arrays shall not be used

Description

Rule Definition

Variable-length arrays shall not be used.

Rationale

When the size of an array declared in a block or function prototype is not an integer constant expression, you specify variable length arrays. Variable length arrays are typically implemented as a variable size object stored on the stack. Using variable length arrays can make it impossible to determine statistically the amount of memory for the stack requires.

If the size of a variable-length array is negative or zero, the behavior is undefined.

If a variable-length array must be compatible with another array type, then the size of the array types must be identical and positive integers. If your array does not meet these requirements, the behavior is undefined.

If you use a variable-length array in a sizeof, it is uncertain if the array size is evaluated or not.

Troubleshooting

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

Check Information

Group: Pointers and Arrays
Category: Required
AGC Category: Required

Version History

expand all