gpt4 book ai didi

c - C99 标准中的哪里说有符号整数溢出是未定义的行为?

转载 作者:行者123 更新时间:2023-12-04 00:10:33 26 4
gpt4 key购买 nike

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

我在 6.2.5 节看到关于无符号整数溢出的评论是明确定义的(参见 Why is unsigned integer overflow defined behavior but signed integer overflow isn't? ):

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 中查看未定义行为,我只在列表中看到这些类似的项目:

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



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

最佳答案

我没有 C99 的副本,但在 C11 标准中,此文本出现在第 6.5 节第 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 以上的特例。

关于c - C99 标准中的哪里说有符号整数溢出是未定义的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36342460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com