Casting a NULL pointer function argument resulted in violation of Misra Rule 11.3
Вопрос
I define a NULL_PTR
as 0U
Then call a function with this NULL_PTR
as argument.
read_some_data(2U, (uint8_t *const) NULL_PTR, (uint8_t *const) NULL_PTR);
Called function prototype:
int16_t read_some_data(const uint8_t id, uint8_t *const data_1, uint8_t *const data_2);
On compilation, Misra raised a rule 11.3 violation error.(A cast should not be performed between a pointer type and an integral type.)
But if I just pass the NULL_PTR
as follows, no violation.
read_some_data(2U, NULL_PTR, NULL_PTR);
Which is the better way to do? Suppress Misra 11.3 rule or just pass the NULL_PTR
without casting?
Решение 3
I used NULL_PTR in my header file to avoid using IAR internal configuration file yvals.h which defines NULL. But that's not an issue since I may have to use yvals.h later due to other reasons.
Whether using NULL or NULL_PTR, I assume that the general consensus is to pass NULL without casting. My function doesn't have any problem in accepting it. This way, I can avoid suppressing Misra 11.3 rule.
Hope I am proceeding the right way.
Другие советы
What is wrong with the Standard 'NULL'?
Why cast if you can avoid it? Cast always makes code a little bit more dirty and hints that there is something hacky performed.
So I would just pass NULL_PTR without casting. After checking function specification that it can accept NULL_PTR as its second parameter!!!
On compilation, Misra raised a rule 11.3 violation error.
Just to be pedantic, MISRA did not raise a violation error, your compiler raised a MISRA rule violation error.
From what you've posted, I'm not convinced the reported violation is correct...
On the other hand, personally I'd define NULL_PTR
as (void *)0u
because (strictly speaking) 0 is not a pointer.