rspec/rules/S943/cfamily/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

98 lines
2.2 KiB
Plaintext

== Why is this an issue?
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
[source,cpp]
----
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
[source,cpp]
----
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 ];
}
----
== Resources
* MISRA C:2004, 17.5 - The declaration of objects should contain no more than 2 levels of pointer indirection
* MISRA {cpp}: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
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Simplify the declaration of [parameter name|parameter number n]
'''
== Comments And Links
(visible only on this page)
=== on 16 Jun 2014, 16:50:57 Freddy Mallet wrote:
\[~ann.campbell.2]could you add a description to this rule spec which only contains code snippets for the time being ? Thanks
endif::env-github,rspecator-view[]