Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.PTR.TO_PTR_TO_PTR

Pointer declaration has more than two levels of indirection.

MISRA C 2012 Rule 18.5: Declarations should contain no more than two levels of pointer nesting

Category: Advisory

Analysis: Decidable, Single Translation Unit

Applies to: C90, C99

Amplification

No more than two pointer declarators should be applied consecutively to a type. Any typedef-name appearing in a declaration is treated as if it were replaced by the type that it denotes.

Rationale

The use of more than two levels of pointer nesting can seriously impair the ability to understand the behaviour of the code, and should therefore be avoided.

Example

typedef int8_t * INTPTR;

void function ( int8_t ** arrPar[ ] )          /* Non-compliant */ 
{
  int8_t ** obj2;                              /* Compliant     */ 
  int8_t *** obj3;                             /* Non-compliant */ 
  INTPTR * obj4;                               /* Compliant     */ 
  INTPTR * const * const obj5;                 /* Non-compliant */ 
  int8_t ** arr[ 10 ];                         /* Compliant     */ 
  int8_t ** ( *parr )[ 10 ];                   /* Compliant     */ 
  int8_t * ( **pparr )[ 10 ];                  /* Compliant     */ 
}

struct s 
{
  int8_t * s1;                                 /* Compliant     */ 
  int8_t ** s2;                                /* Compliant     */ 
  int8_t *** s3;                               /* Non-compliant */ 
};

struct s * ps1;                                /* Compliant     */ 
struct s ** ps2;                               /* Compliant     */ 
struct s *** ps3;                              /* Non-compliant */

int8_t ** ( *pfunc1 )( void );                 /* Compliant     */ 
int8_t ** ( **pfunc2 )( void );                /* Compliant     */ 
int8_t ** ( ***pfunc3 )( void );               /* Non-compliant */ 
int8_t *** ( **pfunc4 )( void );               /* Non-compliant */
Note:
  • arrPar is of type pointer to pointer to pointer to int8_t because parameters declared with array type are converted to a pointer to the initial element of the array — this is three levels and is non-compliant;
  • arr is of type array of pointer to pointer to int8_t — this is compliant;
  • parr is of type pointer to array of pointer to pointer to int8_t — this is compliant;
  • pparr is of type pointer to pointer to array of pointer to int8_t — this is compliant.

MISRA-C 2004 Rule 17.5 (advisory): The declaration of objects should contain no more than 2 levels of pointer indirection

Pointer declaration has more than two levels of indirection.

Use of more than 2 levels of indirection can seriously impair the ability to understand the behaviour of the code, and should therefore be avoided.

Example

typedef int8_t * INTPTR;

struct s {
   int8_t * s1;                               /* compliant     */
   int8_t ** s2;                              /* compliant     */
   int8_t *** s3;                             /* not compliant */
};

struct s * ps1;                               /* compliant     */
struct s ** ps2;                              /* compliant     */
struct s *** ps3;                             /* not compliant */

int8_t ** ( *pfunc1)();                       /* compliant     */
int8_t ** ( **pfunc2)();                      /* compliant     */
int8_t ** (***pfunc3)();                      /* not compliant */
int8_t *** ( **pfunc4)();                     /* not compliant */

void function( int8_t * par1,
               int8_t ** par2,
               int8_t *** par3,               /* not compliant */
               INTPTR * par4,
               INTPTR * const * const par5,   /* not compliant */
               int8_t * par6[],
               int8_t ** par7[])              /* not compliant */
{
   int8_t * ptr1;
   int8_t ** ptr2;
   int8_t *** ptr3;                           /* not compliant */
   INTPTR * ptr4;
   INTPTR * const * const ptr5;               /* not compliant */
   int8_t * ptr6[10];
   int8_t ** ptr7[10];
}

Explanation of types:

  • par1 and ptr1 are of type pointer to int8_t.
  • par2 and ptr2 are of type pointer to pointer to int8_t.
  • par3 and ptr3 are of type pointer to a pointer to a pointer to int8_t. This is three levels and is not compliant.
  • par4 and ptr4 are expanded to a type of pointer to a pointer to int8_t.
  • par5 and ptr5 are expanded to a type of const pointer to a const pointer to a pointer to int8_t. This is three levels and is not compliant.
  • par6 is of type pointer to pointer to int8_t because arrays are converted to a pointer to the initial element of the array.
  • ptr6 is of type pointer to array ofint8_t .
  • par7 is of type pointer to pointer to pointer to int8_t because arrays are converted to a pointer to the initial element of the array. This is three levels and is not compliant.
  • ptr7 is of type array of pointer to pointer to int8_t. This is compliant.

MISRA-C++ 2008 Rule 5-0-19 (required): The declaration of objects shall contain no more than two levels of pointer indirection.

Rationale

Use of more than two levels of indirection can seriously impair the ability to understand the behaviour of the code, and therefore should be avoided.

Example

typedef int8_t * INTPTR;

struct s {
   int8_t * s1;            // Compliant
   int8_t ** s2;           // Compliant
   int8_t *** s3;          // Non-compliant
};

struct s * ps1;            // Compliant
struct s ** ps2;           // Compliant
struct s *** ps3;          // Non-compliant

int8_t ** ( *pfunc1)();    // Compliant
int8_t ** ( **pfunc2)();   // Compliant
int8_t ** (***pfunc3)();   // Non-compliant
int8_t *** ( **pfunc4)();  // Non-compliant

void function( int8_t * par1,                  // Compliant
               int8_t ** par2,                 // Compliant
               int8_t *** par3,                // Non-compliant
               INTPTR * par4,                  // Compliant
               INTPTR * const * const par5,    // Non-compliant
               int8_t * par6[],                // Compliant
               int8_t ** par7[])               // Non-compliant
{
   int8_t * ptr1;                  // Compliant
   int8_t ** ptr2;                 // Compliant
   int8_t *** ptr3;                // Non-compliant
   INTPTR * ptr4;                  // Compliant
   INTPTR * const * const ptr5;    // Non-compliant
   int8_t * ptr6[ 10 ];            // Compliant
   int8_t ** ptr7[ 10 ];           // Compliant
}

Explanation of types

  • 'par1' and 'ptr1' are of type pointer to 'int8_t'.
  • 'par2' and 'ptr2' are of type pointer to pointer to 'int8_t'.
  • 'par3' and 'ptr3' are of type pointer to a pointer to a pointer to 'int8_t'. This is three levels and is non-compliant.
  • 'par4' and 'ptr4' are expanded to a type of pointer to a pointer to 'int8_t'.
  • 'par5' and 'ptr5' are expanded to a type of const pointer to a const pointer to a pointer to 'int8_t'. This is three levels and is non-compliant.
  • 'par6' is of type pointer to pointer to 'int8_t' because arrays are converted to a pointer to the initial element of the array.
  • 'ptr6' is of type pointer to array of 'int8_t'.
  • 'par7' is of type pointer to pointer to pointer to 'int8_t' because arrays are converted to a pointer to the initial element of the array. This is three levels and is non-compliant.
  • 'ptr7' is of type array of pointer to pointer to 'int8_t'. This is compliant.