I encountered a warning related to MISRA 2012 Rule 11.4, which flags casting from a pointer to an integer. I attempted to use intptr_t
, but it did not resolve the issue. Could anyone provide guidance on how to use it safely? A sample code demonstration would be helpful in resolving the MISRA warning.
Below is the sample code that I implemented, which is triggering the MISRA 2012 Rule 11.4 warning related to casting a pointer to an integer:
float buffer[SIZE];
int32_t casted_ptr;
casted_ptr=(int32_t)&buffer[0];
I encountered a warning related to MISRA 2012 Rule 11.4, which flags casting from a pointer to an integer. I attempted to use intptr_t
, but it did not resolve the issue. Could anyone provide guidance on how to use it safely? A sample code demonstration would be helpful in resolving the MISRA warning.
Below is the sample code that I implemented, which is triggering the MISRA 2012 Rule 11.4 warning related to casting a pointer to an integer:
float buffer[SIZE];
int32_t casted_ptr;
casted_ptr=(int32_t)&buffer[0];
Share
Improve this question
edited Mar 12 at 12:32
Mrk234
asked Mar 12 at 7:58
Mrk234Mrk234
655 bronze badges
9
|
Show 4 more comments
1 Answer
Reset to default 4Rule 11.4 is advisory and it's one of those rules that are "very advisory", as you won't be able to follow it in practice when doing any form of hardware-related programming. The rule simply does not allow any such casts, period.
The rationale given with the rule covers alignment and integer presentation. Other potential problems not mentioned are incorrect addresses and strict aliasing violations.
So basically, it is OK to break this advisory rule if you have concluded that:
- The cast does not lead to misalignment
- The cast does not lead to a value which cannot fit/cannot be represented
- The address leads to valid memory, for example to where a hardware peripheral register resides
- A pointer type does not get de-referenced while it points to something which has a different type than the de-referenced pointer
intptr_t
is still a integer type, it will not change anything. Also, MISRA 2012 Rule 11.4 explicitely mentions that these types are also not permitted by that rule. – Gerhardh Commented Mar 12 at 8:06