How to eliminate MISRA 11.3 warning with 2D Array Argument?
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
}
0 Kommentare
Antworten (1)
Yash
am 4 Okt. 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.
Siehe auch
Kategorien
Mehr zu Coding Standards and Code Metrics finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!