在C99标准中,哪里表明有符号整数溢出是未定义的行为? [英] Where in the C99 standard does it say that signed integer overflow is undefined behavior?

查看:70
本文介绍了在C99标准中,哪里表明有符号整数溢出是未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C99标准中,哪里表示有符号整数溢出是未定义的行为?

Where in the C99 standard does it say that signed integer overflow is undefined behavior?

我看到有关无符号整数溢出定义良好的评论(请参阅为什么未签名的整数溢出定义了行为但为什么签名了整数)在第6.2.5节中:

I see the comment about unsigned integer overflow being well-defined (see Why is unsigned integer overflow defined behavior but signed integer overflow isn't?) in section 6.2.5:


涉及无符号操作数的计算永远不会溢出,
,因为无法用所得的无符号整数类型表示的结果是
,其模数比所得类型表示的
的最大值要大1。

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

但是我在附录J中查看未定义的行为,我只在列表中看到以下类似项目:

but I'm looking in Appendix J on undefined behaviors, and I only see these similar items in the list:


已将具有提升型符号的表达式左移,并且其中一个
表达式的e值为负,或者移位的结果在
提升类型中无法表示

An expression having signed promoted type is left-shifted and either the value of the expression is negative or the result of shifting would be not be representable in the promoted type


整数算术或转换函数的结果值不能表示为

The value of the result of an integer arithmetic or conversion function cannot be represented

(请注意,这是指整数算术函数,而不是整数算术本身

(note this refers to "an integer arithmetic function", not integer arithmetic itself

推荐答案

我没有C99的副本,但在C11标准中,此文本显示在6.5节的第5段中:

I don't have a copy of C99, but in the C11 standard this text appears in Section 6.5, paragraph 5:


如果在表达式的求值过程中发生异常情况(即,如果数学上未定义结果或该结果不在其类型的可表示值范围内),则行为不确定。

If an exceptional condition occurs during the evaluation of an expression (that is, if the result is not mathematically defined or not in the range of representable values for its type), the behavior is undefined.

对于任何溢出似乎都是万能的;然后,有关无符号整数的文本将成为6.5¶5以上的特殊情况。

Which would seem to be a catch-all for any overflow; the text about unsigned integers then becomes a special-case above 6.5 ¶ 5.

这篇关于在C99标准中,哪里表明有符号整数溢出是未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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