Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-943

Object declarations should contain no more than 2 levels of pointer indirection

    Details

    • Message:
      Simplify the declaration of [parameter name|parameter number n]
    • Default Severity:
      Critical
    • Impact:
      High
    • Likelihood:
      Low
    • Covered Languages:
      C, C++, Objective-C
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      15min
    • MISRA C 2004:
      17.5
    • MISRA C 2012:
      18.5
    • MISRA C++ 2008:
      5-0-19
    • PC-Lint:
      9025

      Description

      While they are extraordinarily useful, pointers are not the most intuitive concept in the world. Pointers to pointers are even harder to understand and use correctly. And with each additional level of indirection, pointer variables become more difficult to use correctly. Therefore pointer declarators should be limited to no more than two levels of nesting.

      Noncompliant Code Example

      typedef int * INTPTR;
      struct s {
       int ** s1;
       int *** s2; // Noncompliant
      };
      
      struct s ** ps1;
      struct s *** ps2; // Noncompliant
      
      int ** ( *pfunc1)();
      int ** ( **pfunc2)();
      int ** (***pfunc3)(); // Noncompliant
      int *** ( **pfunc4)(); // Noncompliant
      
      void function( int ** par1,
                     int *** par2, // Noncompliant
                     INTPTR * par3,
                     int * par4[],
                     int ** par5[]) // Noncompliant
      {
        int ** ptr1;
        int *** ptr2; // Noncompliant
        INTPTR * ptr3;
        int * ptr4[ 10 ];
        int ** ptr5[ 10 ]; //Noncompliant
      }
      

      Compliant Solution

      typedef int * INTPTR;
      struct s {
       int ** s1;
       int ** s2;
      };
      
      struct s ** ps1;
      struct s ** ps2;
      
      int ** (*pfunc1)();
      int ** (**pfunc2)();
      int ** (**pfunc3)();
      int ** (**pfunc4)();
      
      void function( int ** par1,
                     int ** par2,
                     INTPTR * par3,
                     int * par4[],
                     int * par5[])
      {
        int ** ptr1;
        int ** ptr2;
        INTPTR * ptr3;
        int * ptr4[ 10 ];
        int * ptr5[ 10 ];
      }
      

      See

      • MISRA C:2004, 17.5 - The declaration of objects should contain no more than 2 levels of pointer indirection
      • MISRA C++:2008, 5-0-19 - The declaration of objects shall contain no more than two levels of pointer indirection
      • MISRA C:2012, 18.5 - Declarations should contain no more than two levels of pointer nesting

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                Anonymous
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: