是否INT_MIN%-1产生不确定的行为? [英] Does INT_MIN % -1 produce undefined behavior?

查看:138
本文介绍了是否INT_MIN%-1产生不确定的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GCC产生浮code引发 SIGFPE 以下code:

gcc generates floating code that raises SIGFPE for the following code:

#include <limits.h>
int x = -1;
int main()
{
    return INT_MIN % x;
}

不过,我可以找到标准中没有声明这code调用未定义或实现定义的行为。至于我可以告诉大家,它需要返回0。这是海湾合作委员会中的一个bug还是我失去了一些特殊例外的标准使?

However I can find no statement in the standard that this code invokes undefined or implementation-defined behavior. As far as I can tell, it's required to return 0. Is this a bug in gcc or am I missing some special exception the standard makes?

推荐答案

您可能是正确的,这可以被视为在实际标准的错误。该目前的草案解决了这个问题:

You are probably right that this can be considered as a bug in the actual standard. The current draft addresses this problem:

如果商A / B重新presentable,
  恩pression(A / B)* B + A%B款
  等于一个;否则,行为
  两个A / B和一个%b为未定义。

If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a; otherwise, the behavior of both a/b and a%b is undefined.

这篇关于是否INT_MIN%-1产生不确定的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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