使用-fwrapv时,gcc中的签名溢出仍然是未定义的行为吗? [英] Is signed overflow still undefined behaviour in gcc when -fwrapv is used?

查看:80
本文介绍了使用-fwrapv时,gcc中的签名溢出仍然是未定义的行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,已签名的溢出是未定义的行为.

By default signed overflow is undefined behaviour.

我对gcc的理解(基于 https://gcc.gnu .org/onlinedocs/gcc/Code-Gen-Options.html -fwrapv的作用?)是使用-fwrapv使gcc将签名溢出视为定义良好的行为.

My understanding of gcc (based on https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html and What does -fwrapv do?) was that using -fwrapv made gcc treat signed overflow as being well defined behaviour.

但是,对另一个

However, comments on another question seem to say that signed overflow is still undefined behaviour even when this flag is on.

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

Is signed overflow well defined in gcc with -fwrapv? If not, then what is the purpose of -fwrapv?

推荐答案

给出 GCC文档说:

-fwrapv

此选项指示编译器假定使用二进制补码表示法来进行加,减,乘的有符号算术溢出.

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

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

I'd characterize that as an implementation-specific extension that provides clearly defined behavior for what otherwise would be undefined behavior in standard C - if and only if the underlying hardware behaves that way.

就目前而言,我会说这仍然是C标准未定义的行为,但是您是在指示编译器以一种特定的,不可移植的但可预测的方式运行.

Pedantically, I'd say it's still undefined behavior by the C standard, but you're instructing the compiler to act in a specific, non-portable but predictable manner.

这篇关于使用-fwrapv时,gcc中的签名溢出仍然是未定义的行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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