gpt4 book ai didi

c - "Undefined behaviour"总是未定义?

转载 作者:太空狗 更新时间:2023-10-29 16:12:25 25 4
gpt4 key购买 nike

C 中可能有一些东西会导致 UB。

他们中的大多数都可以这样做,但有几个实现定义的行为会更合乎逻辑。让我举个例子:

关于 <<运营商,

If E1 has a signed type and nonnegative value, and E1 × 2 E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

也就是说

signed char r = 0x40;
r <<= 2;

是 UB,IB 在 IMO 中更合乎逻辑。

是否允许实现定义此处发生的情况?

最佳答案

来自 C 标准:

A conforming hosted implementation shall accept any strictly conforming program... A conforming implementation may have extensions (including additional library functions), provided they do not alter the behavior of any strictly conforming program.

换句话说,实现可以自由地保证特定的行为,否则将是未定义的行为。它无法以改变定义的行为的方式做到这一点。

关于c - "Undefined behaviour"总是未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24178996/

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