[previous] MISRA C:2012  Rule-18.1:  (Required) [next] A pointer resulting from arithmetic on a pointer operand shall address an element of the same array as that pointer operand

Amplification:

Creation of a pointer to one beyond the end of the array is well-defined by The Standard and is permitted by this rule. Dereferencing a pointer to one beyond the end of an array results in undefined behaviour and is forbidden by this rule.

This rule applies to all forms of array indexing:

integer_expression + pointer_expression
pointer_expression + integer_expression
pointer_expression - integer_expression
pointer_expression += integer_expression
pointer_expression -= integer_expression
++pointer_expression
pointer_expression++
--pointer_expression
pointer_expression--
pointer_expression [ integer_expression ]
integer_expression [ pointer_expression ]

Note: a subarray is also an array.

Note: for purposes of pointer arithmetic, The Standard treats an object that is not a member of an array as if it were an array with a single element (C90 Section 6.3.6, C99 Section 6.5.6).

Example Code:


#pragma PRQA_MESSAGES_OFF 2982,2983

#include "misra.h"
#include "m3cmex.h"


extern int16_t rule_1801( void )
{
   int16_t lot[10];
   int16_t *p;

   p = &lot[20];               /* 2930      */

   p = &s16a;
   p = p + 10;                 /* 2931 0488 */

   if (s16a > 10)
   {
   }

   p = &lot[s16a];             /* 2932      */

   return 0;
}


QAC messages that encompass this guideline:

2930 Constant: Computing an invalid pointer value.
2931 Definite: Computing an invalid pointer value.
2932 Apparent: Computing an invalid pointer value.



(c) The Motor Industry Research Association, 2012
QA C Source Code Analyser 8.1.2
MISRA C:2012 Compliance Module 1.0
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents