C/C++ 无符号整数溢出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16056758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:02:39  来源:igfitidea点击:

C/C++ unsigned integer overflow

c++c

提问by Detective King

i'm reading an article about integer security . here's the link: http://ptgmedia.pearsoncmg.com/images/0321335724/samplechapter/seacord_ch05.pdf

我正在阅读一篇关于整数安全性的文章。这是链接:http: //ptgmedia.pearsoncmg.com/images/0321335724/samplechapter/seacord_ch05.pdf

In page 166,there is said:

在第 166 页中说:

A computation involving unsigned operands can never over?ow,because a result that cannot be represented by the resulting unsigned integer type is reduced modulo to the number that is one greater than the largest value that can be represented by the resulting type.

涉及无符号操作数的计算永远不会溢出,因为无法由结果无符号整数类型表示的结果被取模减少到比结果类型可以表示的最大值大 1 的数字。

What does it mean? appreciate for reply.

这是什么意思?感谢您的回复。

回答by Pubby

It means the value "wraps around".

这意味着该值“环绕”。

UINT_MAX + 1 == 0
UINT_MAX + 2 == 1
UINT_MAX + 3 == 2

.. and so on

.. 等等

As the link says, this is like the modulo operator: http://en.wikipedia.org/wiki/Modulo_operation

正如链接所说,这就像模运算符:http: //en.wikipedia.org/wiki/Modulo_operation

回答by Sebastian Mach

No overflow?

没有溢出?

"Overflow" here means "producing a value that doesn't fit the operand". Because arithmetic modulo is applied, the value always fits the operand, therefore, no overflow.

“溢出”在这里的意思是“产生一个不适合操作数的值”。由于应用了算术模数,因此该值始终适合操作数,因此不会溢出。

In other words, before overflow can actually happen, C++ will already have truncated the value.

换句话说,在溢出实际发生之前,C++ 已经截断了该值。

Modulo?

模数?

Taking a value modulo some other value means to apply a division, and taking the remainder.

取一个值以其他值为模意味着应用除法,然后取余数。

For example:

例如:

0 % 3 = 0  (0 / 3 = 0, remainder 0)
1 % 3 = 1  (1 / 3 = 0, remainder 1) 
2 % 3 = 2  (2 / 3 = 0, remainder 2)
3 % 3 = 0  (3 / 3 = 1, remainder 0)
4 % 3 = 1  (4 / 3 = 1, remainder 1)
5 % 3 = 2  (5 / 3 = 1, remainder 2)
6 % 3 = 0  (6 / 3 = 2, remainder 0)
...

This modulo is applied to results of unsigned-only computations, with the divisor being the maximum value the type can hold. E.g., if the maximum is 2^16=32768, then 32760 + 9 = (32760 + 9) % (32768+1) = 0.

此模应用于仅无符号计算的结果,除数是该类型可以容纳的最大值。例如,如果最大值为 2^16=32768,则32760 + 9 = (32760 + 9) % (32768+1) = 0.

回答by Mats Petersson

It means that you can't alter the sign of a unsignedcalculation, but it can still produce unexpected results. Say we have an 8-bit unsigned value:

这意味着您不能改变unsigned计算的符号,但它仍然可以产生意想不到的结果。假设我们有一个 8 位无符号值:

 uint8_t a = 42;

and we add 240 to that:

我们将 240 添加到其中:

 a += 240;

it will not fit, so you get 26.

它不适合,所以你得到 26。

Unsigned math is clearly defined in C and C++, where signed math is technically either undefined or implementation dependent or some other "things that you wouldn't expect may happen" wording (I don't know the exact wording, but the conclusion is that "you shouldn't rely on the behaviour of overflow in signed integer values")

无符号数学在 C 和 C++ 中有明确定义,其中有符号数学在技术上要么是未定义的,要么是依赖于实现的,或者是其他一些“你不希望可能发生的事情”的措辞(我不知道确切的措辞,但结论是“你不应该依赖有符号整数值的溢出行为”)