gpt4 book ai didi

c - 当使用 -fwrapv 时,签名溢出在 gcc 中仍然是未定义的行为吗?

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

背景

默认情况下,有符号溢出是未定义的行为。

我对 gcc 的理解(基于 ​​https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.htmlWhat does -fwrapv do? )是使用 -fwrapv 使 gcc 将签名溢出视为定义明确的行为。

然而,评论另一个question似乎是说即使这个标志打开,签名溢出仍然是未定义的行为。

问题

gcc 中是否使用 -fwrapv 明确定义了签名溢出?如果不是,那么 -fwrapv 的目的是什么?

最佳答案

给定the GCC documentation说:

-fwrapv

This option instructs the compiler to assume that signed arithmetic overflow of addition, subtraction and multiplication wraps around using twos-complement representation.

我将其描述为特定于实现的扩展,它为标准 C 中未定义的行为提供明确定义的行为 - 当且仅当底层硬件以这种方式运行时。

迂腐地说,我会说它仍然是 C 标准未定义的行为,但您是在指示编译器以特定的、不可移植但可预测的方式行事。

关于c - 当使用 -fwrapv 时,签名溢出在 gcc 中仍然是未定义的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54953219/

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