admin管理员组文章数量:1393367
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
本文标签: MISRA CIs it safe to cast pointer to int type in cStack Overflow
版权声明:本文标题:MISRA C-Is it safe to cast pointer to int type in c - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744765194a2623996.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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