How to eliminate MISRA 11.3 warning with 2D Array Argument?

15 visualizaciones (últimos 30 días)
Moses Fridman
Moses Fridman el 22 de Sept. de 2023
Comentada: Moses Fridman el 6 de Oct. de 2023
Consider this sample code. I want to pass a 2-d array in to a function as a non modifyable argument.
If I use this notation, I get a MISRA warning 11.3 " A cast shall not be performed between a pointer to object type and a pointer to a different object type. " . If I leave out the const keyword, I get a MISRA warning that pointer arguments should be constant if not modified.
Is there a simple way to do this and eliminate the 11.3 warning?
typedef signed short int S16; /* ss_ */
typedef unsigned char U8; /* uc_ */
static void TEST_FUNC(const S16 loc_testVar[4][4] );
void MAIN(void)
{
/* const */ S16 testVar[4][4] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
TEST_FUNC(testVar); //MISRA warning 11.3 here
}
static void TEST_FUNC(const S16 loc_testVar[4][4])
{
U8 i, j;
S16 newVar[4][4];
for (i=0; i<4; i++)
{
for (j=0; j<4; j++)
{
newVar[i][j] = loc_testVar[i][j];
}
}
loc_testVar[1][1] = 3; // this should cause an error if loc_testVar is actualy constant!
}

Respuestas (1)

Yash
Yash el 4 de Oct. de 2023
The best way to address the issue and eliminate the MISRA warning 11.3 is to make the array constant. By declaring the array as constant, you clearly indicate that you don't intend to modify it within the function, which aligns with MISRA guidelines and ensures safety and maintainability of your code.
So, in your code, you can declare the "testVar" array as constant like this:
const S16 testVar[4][4] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
This change will eliminate the warning.
You can read more about the MISRA C rule 11.3 here : https://in.mathworks.com/help/bugfinder/ref/misrac2012rule11.3.html
I hope this helps.
  1 comentario
Moses Fridman
Moses Fridman el 6 de Oct. de 2023
Thanks for your comment, but in my case the 2d array needs to be modifiable, because it’s contents are calculated at runtime. Hence the issue.

Iniciar sesión para comentar.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by