![]() |
![]() |
2931 | ![]() |
||||
![]() | |||||||
| Arrays | |||||||
A definite anomaly has been identified. An invalid pointer address is being generated, either in an array subscript operation or by pointer arithmetic. It is possible for a pointer to address any element inside an array and also the element one beyond the end of an array; but the result of computing the address of any other notional element is an 'invalid pointer'. A scalar object can be considered as equivalent to an array dimensioned to 1. Notice that the action of computing an invalid pointer is classified as undefined behaviour even if the pointer is not actually being dereferenced. For example:
/*PRQA S 488,489,2017,2824,2982,2983,2984,2986,3120,3132,3198,3199,3203,3227,3408,3447,3681,3683 ++*/
extern int a[10];
void f1(int n)
{
int *p;
if (n == -1)
{
p = &a[n]; /* 2931 */
p = a + n; /* 2931 */
}
if (n == 10)
{
p = &a[n]; /* */
p = a + n; /* */
}
if (n == 11)
{
p = &a[n]; /* 2931 */
p = a + n; /* 2931 */
}
}
MISRA-C:2004 Rules applicable to message 2931:
| Rule 17.1 (Required) | Pointer arithmetic shall only be applied to pointers that address an array or array element. |
See also:
![]() | ||
| QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2 © 2012 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index | Contents |