28-09-2004, 03:27 PM
Jonas Hansryd Wrote:Misra C:1998 rule 104 reads "Non-constant pointers to functions shall not be used"
One may interpret this rule as that the only allowed use and declaration of a function pointer is as in the example below
Example:
Code:typedef U16_t( *const MyFuncPointer_t) (U16_t);
U16_t MyFunc(U16_t MyInput)
{
return (MyInput + 2u);
}
MyFuncPointer_t MyFuncPointer = MyFunc;
void main(void)
{
printf("\\nReturn: %i\\n\\n",MyFuncPointer(4u));
}
If the above interpretation is correct, then the function pointer should always be declared as a constant and can never point at another function during runtime.
Rule 105 reads: "All the functions pointed to by a single pointer to function shall be identical in the number and type of parameters and the return type".
We interpret rule 105 as it is allowed for one single function pointer to point at diffferent functions during runtime. But with the conservative interpretation of rule 104, a function pointer may never change the function it points at during runtime and thus conflicts with rule 105, i.e. rule 105 would be superfluous.
What would be the correct way to interpret rule 104 and rule 105?
Rule 105 may be interpreted in another way. See this example:
Code:
#include
typedef int (*const FuncPtrType1)(int);
typedef int (*const FuncPtrType2)(double);
int bar()
{
return 7;
}
FuncPtrType1 funcPtr1 = (FuncPtrType1)bar;
FuncPtrType2 funcPtr2 = (FuncPtrType2)bar;
int main(void)
{
printf("Return: %i\\n", funcPtr1(4));
printf("Return: %i\\n", funcPtr2(5.0));
return 0;
}
Rule 105 is violated since there are two pointers to the function "bar" (both of them single pointers to that function) that are not identical in the type of parameters.
Volker Glave