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

20 次查看(过去 30 天)
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!
}

回答(1 个)

Yash
Yash 2023-10-4
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 个评论
Moses Fridman
Moses Fridman 2023-10-6
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.

请先登录,再进行评论。

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by