![]() |
![]() |
2772 | ![]() |
||||
![]() | |||||||
| Pointers | |||||||
An apparent condition has been detected. Examination of the context indicates that an operation is being performed which will sometimes compare or subtract the value of two pointers which address different objects. Behaviour is undefined if an attempt is made to subtract pointers which do not address elements of the same array or to compare (with a relational operator) pointers which do not point to the same struct, union or array. For example, in the code below, the pointer
p1 will always address buf1, but p2 will sometimes ad dress buf1 and sometimes buf2, depending on the value of the function parameter n.
For Example:
/*PRQA S 488,490,2017,2983,2991,2995,3120,3132,3199,3203,3227,3355,3358,3408,3447 ++*/
extern int buf1[10];
extern int buf2[10];
extern void f2772(int n)
{
int * p1;
int * p2;
long d;
p1 = buf1;
p2 = &buf1[5];
if (n > 0)
{
p2 = buf2;
}
d = p2 - p1; /* 2772 */
if (p2 > p1) /* 2772 */
{
}
}
MISRA C:2012 Rules applicable to message 2772:
| Rule-18.2 (Required) | Subtraction between pointers shall only be applied to pointers that address elements of the same array |
| Rule-18.3 (Required) | The relational operators >, >=, < and <= shall not be applied to objects of pointer type except where they point into the same object |
See also:
![]() | ||
| QA·C Source Code Analyser 8.1
MISRA C:2012 Compliance Module 1.0 © 2012 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index | Contents |