Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function returning pointer to array - Rule 17.4
#6
The MISRA C working group agrees that this is a problem with the current wording for Rule 17.4. The problem is not limited to returns from functions.

For example:
Code:
int_32 * select_row (int_32 arr[3][2], uint_32 index)
{
  return arr[index];
}

int_32 array[3][2] = {{1, 2}, {3, 4}, {5, 6}};
uint_32 i;

for ( i = 0U; i < 3U; i++)
{
   int_32 *row = select_row(array, i);

   /* or int_32 *row = array[i];  */
  
   ... row[0] ....  /* violates 17.4 */
  
}
In many cases, returning a pointer to an array is best avoided as suggested by a previous response to this question. However, it is appreciated that slicing an array may be required and it may be more readable to use a pointer to the slice. For this reason, the next version of the MISRA C guidelines to likely to allow such indexing.

Indexing such slices is non-compliant in this version of MISRA C and will need a deviation.
Posted by and on behalf of the MISRA C Working Group
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)