Main Content

MISRA C:2012 Rule 18.10

Pointers to variably-modified array types shall not be used

Since R2025a

Description

Rule Definition

Pointers to variably-modified array types shall not be used.1

Rationale

If you use pointers to variably modified array types to declare an object or a parameter, the compatibility of the object and the array type cannot be determined in compile time. For example, in this code, the variably modified array type parameter array is used for declaring the array myarray. The two variably modified array type objects are compatible only if the value of the parameter length equals 20. The compatibility of these arrays cannot be determined at compile time.

void foo(int length, int (*array)[length]){
    int (*myarray)[20];
    myarray = array;
}
If the value of the parameter length is not 20, the behavior of this code is undefined. To avoid undefined behavior, do not use pointers to variably modified array types.

Polyspace Implementation

Polyspace® reports a violation is a pointer to a variably modified array type is used for declaring an object or a function parameter.

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

In this example, the function matrixMultiply() accepts three pointers to variably modified array types as parameters. The size of these arrays cannot be determined in compile time. Polyspace reports violation of this rule.

#include <stdlib.h>

// Function to perform matrix multiplication
void matrixMultiply(int rowsA, int colsA, int colsB, 
                    int (*matrixA)[colsA],  //Noncompliant
                    int (*matrixB)[colsB],  //Noncompliant
                    int (*result)[colsB]) { //Noncompliant 
    for (int i = 0; i < rowsA; ++i) {
        for (int j = 0; j < colsB; ++j) {
            result[i][j] = 0;
            for (int k = 0; k < colsA; ++k) {
                result[i][j] += matrixA[i][k] * matrixB[k][j];
            }
        }
    }
}

Check Information

Group: Pointers and arrays
Category: Mandatory
AGC Category: Mandatory

Version History

Introduced in R2025a


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.