About Rule 5-0-15 and the size of the array

Moderators: david ward, misra cpp

Post Reply
[email protected]
Posts: 2
Joined: Thu Nov 02, 2017 5:49 pm
Company: NXP Semiconductors

About Rule 5-0-15 and the size of the array

Post by [email protected] » Mon Mar 25, 2019 5:32 pm

Hi,
I have a question about Rule 5-0-15: "Array indexing shall be the only for of pointer arithmetic".

Is the rule satisfied if the code referencing the array element has no knowledge of the dimensions of the array object at compile/link time?

For example, in the case where the argument passed to parameter "p2" of "my_fn" is an array whose size is defined at runtime?

Code: Select all

void my_fn ( uint8_t * p1, uint8_t p2[ ] )
{
   uint8_t index = 0;
   uint8_t * p3;
   uint8_t * p4;
   *p1 = 0;
   ++index;
   index = index + 5;
   p1      = p1 + 5;   // Non-compliant – pointer increment
   p1[ 5 ] = 0;        // Non-compliant – p1 was not declared as array
   p3      = &p1[ 5 ]; // Non-compliant – p1 was not declared as array

   p2[ 0 ]     = 0;
   p2[ index ] = 0;        // Compliant
   p4          = &p2[ 5 ]; // Compliant
}
uint8_t a1[ 16 ];
uint8_t a2[ 16 ];

my_fn ( a1, a2 );
my_fn ( &a1[ 4 ], &a2[ 4 ] );

misra cpp
Posts: 145
Joined: Mon Jun 02, 2008 1:55 pm
Company: MISRA

Re: About Rule 5-0-15 and the size of the array

Post by misra cpp » Wed Apr 10, 2019 2:46 pm

Yes, knowledge of the array dimensions is not required
Posted by and on behalf of
the MISRA C++ Working Group

Post Reply

Return to “6.5 Expressions (C++)”