【发布时间】:2016-03-01 03:05:03
【问题描述】:
这是我在尝试将两个数字相加时检测是否发生无符号整数溢出的实现。
我的系统上 unsigned int (UINT_MAX) 的最大值是 4294967295。
int check_addition_overflow(unsigned int a, unsigned int b) {
if (a > 0 && b > (UINT_MAX - a)) {
printf("overflow has occured\n");
}
return 0;
}
这似乎适用于我尝试过的值。
任何流氓案件?你认为有什么好处和坏处?
【问题讨论】:
-
您的代码接受
int,但您的文字是在谈论unsigned int...它是什么? -
此代码无法编译。使用
{而不是[。 -
@Kingamere 你的函数有
int返回类型并且不返回任何东西。说真的! -
你必须返回一些值,但你没有。因此,即使错字被纠正。这个功能在任何情况下都不起作用。
-
@Kingamere 你能解释一下为什么
a > 0条件吗?
标签: c unsigned integer-overflow unsigned-integer