c++ - C++中的自动溢出检测?

标签 c++ overflow

<分区>

Possible Duplicate:
Best way to detect integer overflow in C/C++

当我在 C++ 中使用大量数字编写代码时,我常常无法判断何时发生溢出,即使我使用的是 long long 或其他 64 位数据类型。有没有比观察错误值更有效的方法来检测何时发生溢出?

最佳答案

您从标准 C++ 中获得的可能不多:

5 Expressions

4 If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. [ Note: most existing implementations of C++ ignore integer overflows. Treatment of division by zero, forming a remainder using a zero divisor, and all floating point exceptions vary among machines, and is usually adjustable by a library function. —end note ]

您最好的选择是使用 <cstdint> 中定义的标准固定宽度整数类型例如 uint32_t .

看看 <cerrno>标题也是错误代码,例如 EOVERFLOW .然后是overflow_error/underflow_error来自 <stdexcept> 的类(class).

关于c++ - C++中的自动溢出检测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10865443/

相关文章:

c++ - 将继承重构为组合,在 C++ 中保持多态能力

c++ - OpenGL glTranslate 和 glRotate

c++ - sgx_ecc256_create_key_pair 失败

CSS:溢出的 block 元素一直没有样式

html - 为什么流体容器内的一行会在 x 轴上溢出?

google-chrome - Google Chrome 中的溢出隐藏和输入文本滚动

javascript - 检查 HTML 元素是否有滚动条

c++ - 这两个数组之间有区别吗?

c++ - 使用异步boost asio代码进行同步操作

overflow - 具有无单位行高的 Html anchor 高度问题