MISRA C++:2008 Rule 5-0-19
The declaration of objects shall contain no more than two levels of pointer indirection
Description
Rule Definition
The declaration of objects shall 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 of- varis- constpointer to a- constpointer to a pointer to- char, which is three levels of pointer indirection. The declaration of- var2has 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 of- arrParamis a pointer to a pointer to a pointer to- char(three levels of pointer indirection). The declaration of- arrVaris compliant because- arrVarhas 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: Expressions | 
| Category: Required | 
Version History
Introduced in R2013b
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.