我有以下代码部分:
----------header--------------------- typedef volatile struct REG_Base{ a; b; }REG_t #define address (0xFFF45556) ------------------------------------ --------Source----------------------- LOCAL REG_t *pToREG; pToREG= (REG_t *) address; -------------------------------------我在最后一行收到了MISRA消息"在指向易失对象的指针和整数类型之间的投射".
I got on the last line the MISRA message "Cast between a pointer to volatile object and an integral type".
您知道如何避免此消息吗?
Any idea how to avoid this message?
谢谢!
推荐答案MISRA有一个咨询规则,该规则禁止从整数到指针的强制转换.理由是他们担心在整数不能表示指针或未对齐的情况下所涉及的行为定义不明确.
MISRA has an advisory rule which bans casts from integers to pointers. The rationale is that they are concerned with the poorly defined behavior involved in case the integer cannot represent the pointer, or in case of misalignment.
这是过于脚的规则之一,仅供参考.大多数嵌入式系统都会违反规则.
This is one of the overly pedantic rules and it is just advisory. Most embedded systems will deviate from the rule.
话虽如此,您的代码中包含一些可疑的内容:
That being said, your code contains some questionable things:
- 易失性限定符不应属于typedef.
- a和b声明没有任何意义,它们是否是某种丑陋的宏?
- 将结构映射到物理地址不是可移植且安全的.该结构可能包含填充,对于给定的类型,地址可能未正确对齐.至少,您需要一些方法来确保没有结构填充,最好是静态断言.
- The volatile qualifier should not be part of the typedef.
- The a and b declarations doesn't make any sense, are they some sort of ugly macros?
- Mapping a struct to a physical address is not portable and safe. The struct might contain padding, the address might not be correctly aligned for the given types. At the very least, you need some means to assure there are no struct padding, preferably a static assert.
还请注意,MISRA要求将整数常量写为0xFFF45556u.这不是一个坏主意,因为0xFFF45556的类型为unsigned int,而0x7FFFFFFF的类型为signed int.除非您小心,否则这些事情可能会导致与隐式类型提升有关的细微错误.
Also note that MISRA requires the integer constant to be written as 0xFFF45556u. This is not a bad idea, because 0xFFF45556 is of type unsigned int, while for example 0x7FFFFFFF is of type signed int. These things might lead to subtle bugs related to implicit type promotions unless you are careful.
更多推荐
MISRA:在指向易失对象的指针和整数类型之间进行转换?
发布评论