检测整数溢出 [英] detecting integer overflow

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

问题描述

有没有办法在添加两个整数期间溢出

可以检测到



例如。


假设我们有三个无符号整数,a,b,c。

我们正在检查

if(( a + b)> c)

做点什么;

其他

做点什么。


如果添加a和b生成的溢出,即使

a + b大于c,检查也可能失败。

我们怎样才能避免这种情况失败?


提前获得任何帮助。

Is there any way by which the overflow during addition of two integers
may
be detected ?

eg.

suppose we have three unsigned integers, a ,b, c.
we are doing a check like
if ((a +b) > c)
do something;
else
do something else.

If addition of a and b genearates overflow, the check may fail even if
a + b is larger than c.
How can we avoid such conditions from failure ?

Thanx in advance for any help ....

推荐答案

ju ********** @ yahoo.co.in 写道:
是否有任何方法可以检测到添加两个整数后溢出?
Is there any way by which the overflow during addition of two integers
may
be detected ?




按照标准,我会个人猜测

溢出整数的状态是未定义的。在大多数PC上,虽然我认为

结果总是小于两个值中的较低值

添加 - 这将是一种测试方法。 />

if((c =(a + b))>(a< b?a:b)){

....
}

else {/ *溢出! * /

....

}


我只是说这是我的头脑,并且你不应该相信它直到你已经彻底测试过了。



By the standard, I would personally guess that the state of an
overflown integer is undefined. On most PCs though I would imagine that
the result will always be less than the lower of the two values
added--which would be a way to test.

if ((c = (a + b)) > (a < b ? a : b)) {
....
}
else { /* overflowed! */
....
}

I''m just saying this off the top of my head, and you shouldn''t trust it
until you''ve tested throroughly though.


ju ********** @ yahoo.co.in 写道:
是否有任何方法可以检测到在添加两个整数时溢出?

例如,

假设我们有三个无符号整数, a,b,c。
我们正在检查
如果((a + b)> c)
做某事;
否则
做其他事情。

如果a和b的添加物溢出,即使a / b大于c,检查也可能失败。
我们怎样才能避免这种情况失败?
Is there any way by which the overflow during addition of two integers
may
be detected ?

eg.

suppose we have three unsigned integers, a ,b, c.
we are doing a check like
if ((a +b) > c)
do something;
else
do something else.

If addition of a and b genearates overflow, the check may fail even if
a + b is larger than c.
How can we avoid such conditions from failure ?




如果目的只是为了避免像上面提到的那种情况,那么快速的方法可能是从中推广变量的类型int

使用明确的

类型转换在比较中加倍或浮动。


if(((double)a + b)> c)

做点什么;

其他

做别的事情;


当然是漂浮物也可以溢出,但是当

添加两个或更多整数时不太可能发生。



If the intention is just to avoid a situation like the one mentioned, a
quick approach could be to promote the type of the variables from int
to double or float just inside the comparison by using an explicit
typecast.

if (((double)a+b) > c)
do something;
else
do something else;

Of course a float can overflow too, but it is not likely to happen when
two or more integers are added.


ju ********** @ yahoo.co.in 写道:
是否有任何方法可以检测到在添加两个整数时溢出?

例如。

假设我们有三个无符号整数,a,b,c。
我们正在检查
如果((a + b)> c)
做某事;
其他
做其他事情。

如果a和b的添加物溢出,即使
a + b大于c,检查也可能失败。
我们怎样才能避免这种情况发生失败?

Thanx提前获得任何帮助....
Is there any way by which the overflow during addition of two integers
may
be detected ?

eg.

suppose we have three unsigned integers, a ,b, c.
we are doing a check like
if ((a +b) > c)
do something;
else
do something else.

If addition of a and b genearates overflow, the check may fail even if
a + b is larger than c.
How can we avoid such conditions from failure ?

Thanx in advance for any help ....



我曾经使用的大多数PC都包含整数(所以你可以测试它包装了一个
但是标准明确指出整数

溢出是一个UB。所以它可能会导致一个陷阱(或鼻子恶魔)。


所以一种方式来检查一致的方式,你可以做到以下几点。


假设INT类型..(可以扩展到任何类型)


/ * 0 - >添加& b

非0 - >溢出!!!


检查*仅*溢出。

_So any -ve number返回0._

溢出: a + b> INT_MAX(对于所有a> 0和b> 0)

因为我们不能安全地做(a + b)(因为它可能导致溢出),

我们将不等式改为:b> INT_MAX - 一个


* /


inline int isSumOverFlow(int a,int b)

{

返回> 0&& b> 0&& b> (INT_MAX - a);

}

-

(欢迎) http://www.ungerhu.com/jxh/clc.welcome.txt

( clc FAQ) http://www.eskimo.com/ ~scs / C-faq / top.html


这篇关于检测整数溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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