MISRA C++:2023 Rule 11.3.2
The declaration of an object should contain no more than two levels of pointer indirection
Since R2024b
Description
Rule Definition
The declaration of an object should contain no more than two levels of pointer indirection. 1
Rationale
If you use pointers with more than two levels of indirection, a developer reading the code might find it difficult to understand the behavior of the code.
Polyspace Implementation
Polyspace® flags all declarations of objects that contain more than two levels of pointer indirection.
If you use type aliases, the checker includes pointer indirections from the alias in the evaluation of the level of indirection. For instance, in this code snippet, the declaration of
varis non-compliant. The type ofvarisconstpointer to aconstpointer to a pointer tochar, which is three levels of pointer indirection. The declaration ofvar2has two levels of pointer indirection and is compliant.using ptrToChar = char*; void func() { ptrToChar* const* const var = nullptr; //Non-compliant, 3 levels of indirection char* const* const var2 = nullptr; //Compliant, 2 levels of indirection //... }If you pass an array to a function, the conversion of the array to a pointer to the first element of the array is included in the evaluation of the level of indirection. For instance, in this code snippet, parameter
arrParamis non-compliant. The type ofarrParamis a pointer to a pointer to a pointer tochar(three levels of pointer indirection). The declaration ofarrVaris compliant becausearrVarhas type array of pointer to pointer to char (two levels of pointer indirection).void func(char** arrParam[]) //Non-compliant { //... char** arrVar[5]; //Compliant }
This checker does not flag the use of objects with more than two levels of indirection. For instance, in this code snippet, the declaration of var is non-compliant, but the evaluation of the size of var is compliant.
#include<iostream>
using charToPtr = char*;
void func()
{
charToPtr* const* const var = nullptr; //Non-compliant
std::cout << sizeof(var) << std::endl; //Compliant
}Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Check Information
| Group: Declarators |
| Category: Advisory |
Version History
Introduced in R2024b
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.