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

二次信任 提交于 2020-01-30 07:42:07

问题


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

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:

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.

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

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

and

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


回答1:


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.

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.



来源:https://stackoverflow.com/questions/36342460/where-in-the-c99-standard-does-it-say-that-signed-integer-overflow-is-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!