混合C和C++;代码 在混合C和C++程序中给出以下定义: //in a C file. uint8_t GetSize() { return (uint8_t)something; } //in the header of the C++ file. public: MyClass(){}; private: uint8_t index_m;
下面两行都为我提供了一个静态工具(pc lint) 我试了很多次,但是没有一次能帮我摆脱这个警告,为什么混合C和C++;代码 在混合C和C++程序中给出以下定义: //in a C file. uint8_t GetSize() { return (uint8_t)something; } //in the header of the C++ file. public: MyClass(){}; private: uint8_t index_m;,c++,c,static-analysis,C++,C,Static Analysis,下面两行都为我提供了一个静态工具(pc lint) 我试了很多次,但是没有一次能帮我摆脱这个警告,为什么 index_m = (index_m + 1U) % GetSize(); // this one works index_m = (GetSize() - 1U + index_m) % GetSize();// info 834: operator '-' followed by operator '+' could be confusing witho
index_m = (index_m + 1U) % GetSize(); // this one works
index_m = (GetSize() - 1U + index_m) % GetSize();// info 834: operator '-' followed by operator '+' could be confusing without parentheses [MISRA 2004 Rule 12.1, advisory]
index_m = (uint8_t)(index_m + (uint8_t)1) % GetSize(); // warning 573 : signed-unsigned mix with divide, and: info 732: loss of sign (assignment) ('int' to 'uint8_t' (aka 'unsigned char')
index_m = (uint8_t)(GetSize() - (uint8_t)1 + index_m) % GetSize(); // warning 573 : signed-unsigned mix with divide, and: info 834: operator '-' followed by operator '+' could be confusing without parentheses [MISRA 2004 Rule 12.1, advisory]
index_m = (uint8_t)(index_m + (uint16_t)1) % GetSize(); // warning 573 : signed-unsigned mix with divide, and: info 732: loss of sign (assignment) ('int' to 'uint8_t' (aka 'unsigned char')
index_m = (uint8_t)(GetSize() - (uint16_t)1 + index_m) % GetSize(); // warning 573 : signed-unsigned mix with divide, and: info 834: operator '-' followed by operator '+' could be confusing without parentheses [MISRA 2004 Rule 12.1, advisory]
这是多么痛苦的事啊。。。C
有什么快速解决方法
看完评论后,我也尝试了一下,但没有成功
index_m = (uint8_t)(index_m + (uint32_t)1) % GetSize(); // works
index_m = (uint8_t)(GetSize() - (uint32_t)1 + index_m) % GetSize(); // info 834: operator '-' followed by operator '+' could be confusing without parentheses [MISRA 2004 Rule 12.1, advisory]
这让我摆脱了符号/无符号混合的问题,但这个“运算符”-后跟运算符“+”,仍然很奇怪 在C/C++中,小于
int
的整数在操作中使用后,会立即升级为与int
具有相同符号的整数(+
..)
这是历史性的,有点混乱,但我想最初的目的是限制小整数计算溢出的风险
在索引m+1
中,索引m
将升级为无符号int
,然后符号与1
不匹配,后者是有符号int
因此,在操作完成后,您必须强制执行(取决于警告级别)
有什么快速解决方法
最简单的是:
index_m = (index_m + 1U) % GetSize(); // this one works
递增是可以的,但问题是递减
index_m = (GetSize() - 1U + index_m) % GetSize();// info 834: operator '-' followed by operator '+' could be confusing without parentheses [MISRA 2004 Rule 12.1, advisory]
请注意,这只是一条信息消息;不是警告。但无论如何,缺少括号的解决方案是添加括号:
index_m = ((GetSize() - 1U) + index_m) % GetSize();
或者,很明显,将操作顺序更改为
(GetSize()+index_m-1U)
,如注释中所示这是您应该使用int
或更大类型的部分原因。对于内置操作,小于int
的所有内容都将转换为int
/无符号int
。我还尝试了大于uint8的uint16。在32位机器中,int
将是int32\u t
。如果需要无符号,则uint32\u t
将是具有内置运算符的最小类型。因为uint16\u t
小于int
(和unsigned int
)…给出了index\u m=(GetSize()-1U+index\u m)%GetSize()的警告代码>,您是否使用index_m=(GetSize()+index_m-1U)%GetSize()进行了实验代码>?不是我在“index_m=(uint8_t)(index_m+(uint8_t)1)%GetSize();”行中所做的吗?@GuillaumeD我更改了建议。@GuillaumeD:注意1U
是一个无符号int
(uint8_t)1
被提升为带符号的int
@JonathanLeffler最初我有不同的建议。评论是关于我的评论是关于纪尧姆D评论中的代码;它使用(uint8_t)1
,这是一个有符号的int
值。然而,如果你确信我的评论没有帮助(或“评论没有帮助”),请将其标记为“不再需要”,我将在对其他问题和答案的大量评论中隐藏我的懊恼。
index_m = ((GetSize() - 1U) + index_m) % GetSize();