signed int overflow [英] signed int overflow

查看:74
本文介绍了signed int overflow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你知道怎么说* unsigned * overflow是......

well ..已定义。这意味着如果你在它的最大价值上加1,那么你就知道在所有实现后它将具有什么价值




但是你已经签了整数。假设已签名的

整数设置为其最大正值。如果你给它增加1

,会发生什么?:


A)它的实现定义了它的价值

表示例如。它可以回滚到0,或者它可以回滚到最大负数。$ / b

B)未定义的行为。

请说A!

例如:


int main()

{

//在32位机器上

signed int i = 2147483648;


++ i;

}


这只是普通的未定义行为,例如。机器

可以炸掉你的脸上吐出的硝酸,如果它需要

来......


或者是它只是良性地只是实现特定的

值i将在增量后表示?


如果B是这种情况,看起来我打算写类

signed_dof_int,其中自由度=定义溢出。我将使用

模板,因此您可以使用所有完整的

类型。找出特定类型的最大值

的最佳方法是什么?我相信标准库

包含一些全局const变量,比如MAX_INT,

但是我更喜欢我可以在模板中使用的方法。


-JKop

解决方案



" JKop" < NU ** @ NULL.NULL>在消息中写道

你知道如何说*无符号*溢出是......
好...定义。这意味着如果你将其最大值加1,那么你就知道它将在所有实现之后具有什么价值。

但是你已经签署了整数。假设已签名的
整数设置为其最大正值。如果你添加1
,会发生什么?:

A)它的实现定义了它将代表什么价值,例如。它可以回滚到0,或者它可以回滚到最大负数。

B)未定义的行为。

请说A!




Nah..IIRC,它是B.


Sharad




" Sharad Kala" <无****************** @ yahoo.com>在消息中写道

news:2r ************* @ uni-berlin.de ...


" JKop" < NU ** @ NULL.NULL>在消息中写道

你知道如何说*无符号*溢出是......
好...定义。这意味着如果你将其最大值加1,那么你就知道它将在所有实现之后具有什么价值。

但是你已经签署了整数。假设已签名的
整数设置为其最大正值。如果你添加1
,会发生什么?:

A)它的实现定义了它将代表什么价值,例如。它可以回滚到0,或者它可以回滚到最大负数。

B)未定义的行为。

请说A!



Nah..IIRC,它是B.

Sharad




无符号溢出在所有情况下定义?在C ++标准中它做了什么

这么说?


签名溢出的情况很荒谬。标准应该说

溢出被定义为正在执行二进制补码算法。


john


< BLOCKQUOTE>>请说A!




整数值上实现两个'补码算术的机器架构中,你可以将值重新解释为无符号并重新解释它<增量后再次使用
来猜测值可能是多少。这是不需要编译器实现这一点,但逻辑上听起来可能是合理的。


我再说一遍:即使你知道你的平台如何工作,如何机器

关于架构工作的说明,语言并不在意,因为如果确实如此,那么每个平台都需要关注,如果这涉及到

指令序列无法有效实施,对于在这些平台上工作的人来说,这将是很多。因此,未定义

行为。


但是随意更具体地说明你拥有的32位架构

介意和我们可以将非主题的其余部分视为comp.lang.c ++ ;-)


You know how the saying goes that *unsigned* overflow is...
well.. defined. That means that if you add 1 to its maximum
value, then you know exactly what value it will have
afterward on all implementations.

But then you have signed integers. Let''s say a signed
integer is set to its maximum positive value. If you add 1
to it, what happens?:

A) It''s implementation defined what value it will
represent, eg. it could roll back around to 0, or it could
roll back around to the maximum negative number.

B) Undefined behaviour.
Please say A!
For instance:

int main()
{
//on a 32-Bit machine
signed int i = 2147483648;

++i;
}

Is that just plain old undefined behaviour, eg. the machine
can blow up and spit nitric acid in your face if it wants
to...

or is it simply benignly just implementation specific what
value "i" will represent after the incrementation?

If B is the case, it looks like I''m off to write "class
signed_dof_int", where dof = defined overflow. I''ll use a
template for it, so you can do it with all of the integral
types. What''s the best way to figure out the maximum value
of a particular type? I believe that the Standard Library
contains some global const variables, stuff like MAX_INT,
but I''d prefer a method I could use within a template.

-JKop

解决方案


"JKop" <NU**@NULL.NULL> wrote in message

You know how the saying goes that *unsigned* overflow is...
well.. defined. That means that if you add 1 to its maximum
value, then you know exactly what value it will have
afterward on all implementations.

But then you have signed integers. Let''s say a signed
integer is set to its maximum positive value. If you add 1
to it, what happens?:

A) It''s implementation defined what value it will
represent, eg. it could roll back around to 0, or it could
roll back around to the maximum negative number.

B) Undefined behaviour.
Please say A!



Nah..IIRC, it''s B.

Sharad



"Sharad Kala" <no******************@yahoo.com> wrote in message
news:2r*************@uni-berlin.de...


"JKop" <NU**@NULL.NULL> wrote in message

You know how the saying goes that *unsigned* overflow is...
well.. defined. That means that if you add 1 to its maximum
value, then you know exactly what value it will have
afterward on all implementations.

But then you have signed integers. Let''s say a signed
integer is set to its maximum positive value. If you add 1
to it, what happens?:

A) It''s implementation defined what value it will
represent, eg. it could roll back around to 0, or it could
roll back around to the maximum negative number.

B) Undefined behaviour.
Please say A!



Nah..IIRC, it''s B.

Sharad



Is unsigned overflow defined in all cases? Where in the C++ standard does it
say that?

The situation with signed overflow is ridiculous. The standard should say
overflow is defined as if twos complement arithmetic was being performed.

john


> Please say A!

In machine architechture that implements two''s complement arithmetics on
integer values, you can reinterpret the value as unsigned and reinterpret it
again after the increment to have a good guess what the value might be. It''s
not required that compilers implement this however logical it may sound.

I repeat: even if you know how your platform works, how the machine
instructions on the architechture works, the language does not care, because
if it did, every platform would have to care and if that would involve
sequence of instructions which is not implementable efficiently, it would
suck a lot for people working on those platforms. Hence, undefined
behaviour.

But feel free to be more specific which 32 bit architechture you have in
mind and we can treat rest of the thread off-topic to comp.lang.c++ ;-)


这篇关于signed int overflow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆