Warning C26454: Arithmetic overflow: #39;-#39; operation produces a negative unsigned result at compile time (io.5)(警告 C26454:算术溢出:“-操作在编译时产生负的无符号结果 (io.5))
问题描述
代码分析:
ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_HISTORY_TYPE,&CAssignHistoryDlg::OnTcnSelchangeTabHistoryType)
警告 C26454:
<块引用>算术溢出:'-' 运算产生负的无符号结果在编译时(io.5).
TCN_SELCHANGE
的定义是:
#define TCN_FIRST (0U-550U)#define TCN_SELCHANGE (TCN_FIRST - 1)
我看不出我还能做什么!
您试图从一个较小的无符号值中减去一个较大的无符号值,这会导致结果回绕到零.在您的情况下,我假设 TCN_FIRST 定义为 0,因此将 TCN_SELCHANGE 设置为 1 将解决问题.
你也应该使用 constexpr
或 const
而不是定义.
根据 MSDN:
C++ 核心检查中的算术溢出检查
<块引用>C26451 RESULT_OF_ARITHMETIC_OPERATION_CAST_TO_LARGER_SIZE:[operator] 操作结束0 并在编译时产生一个大的无符号数.此警告表明减法运算产生了在无符号上下文中评估的否定结果.这会导致结果超过 0 并产生一个非常大的无符号数,这可能导致意外溢出.
1//示例源:2无符号整数负无符号(){3 常量无符号整数 x = 1u - 2u;//这里报告了 C264544 返回 x;5 }1//更正的来源:2无符号整数负无符号(){3 常量无符号整数 x = 4294967295;//行4 返回 x;5 }
<块引用>
在更正后的源中,为无符号结果分配了一个正值.
Code analysis:
ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_HISTORY_TYPE,
&CAssignHistoryDlg::OnTcnSelchangeTabHistoryType)
Warning C26454:
Arithmetic overflow: '-' operation produces a negative unsigned result at compile time (io.5).
The definition of TCN_SELCHANGE
is:
#define TCN_FIRST (0U-550U)
#define TCN_SELCHANGE (TCN_FIRST - 1)
I can't see what else I can do!
You are trying to subtract a larger unsigned value from a smaller unsigned value and it's causing the result to wrap past zero. In your case, I assume TCN_FIRST is defined as 0 so setting TCN_SELCHANGE to one will fix the problem.
You should also be using constexpr
or const
instead of defines anyway.
According to MSDN:
Arithmetic overflow checks in C++ Core Check
C26451 RESULT_OF_ARITHMETIC_OPERATION_CAST_TO_LARGER_SIZE :[operator] operation wraps past 0 and produces a large unsigned number at compile time. This warning indicates that the subtraction operation produces a negative result which was evaluated in an unsigned context. This causes the result to wrap past 0 and produce a really large unsigned number, which can result in unintended overflows.
1 // Example source:
2 unsigned int negativeunsigned() {
3 const unsigned int x = 1u - 2u; // C26454 reported here
4 return x;
5 }
1 // Corrected source:
2 unsigned int negativeunsigned() {
3 const unsigned int x = 4294967295; // OK
4 return x;
5 }
In the corrected source, a positive value was assigned to the unsigned result.
这篇关于警告 C26454:算术溢出:“-"操作在编译时产生负的无符号结果 (io.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:警告 C26454:算术溢出:“-"操作在编译时产生负的无符号结果 (io.5)
基础教程推荐
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01