标准和编译器:用C整数溢出 [英] Integer overflow in C: standards and compilers

查看:1000
本文介绍了标准和编译器:用C整数溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑,包括适当的标准参考感谢卡尔Norum时。

C标准状态

如果一个前pression的评价(即,如果结果不是数学中重新presentable值的范围定义与否期间的异常情况的发生其型),该行为是不确定的。

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.

是保证整数溢出的某些行为有编译器开关?我想避免鼻恶魔。我特别想强制编译器包装上溢出。

Are there compiler switches that guarantee certain behaviors on integer overflow? I'd like to avoid nasal demons. In particular, I'd like to force the compiler to wrap on overflow.

有关唯一性的着想,让我们的标准是C99和编译器GCC会。但是,我有兴趣为其他编译器(ICC,CL)和其他标准(C1X,C89)的答案。事实上,就惹恼C / C ++的人群中,我会为C ++ 0x的甚至AP preciate答案,C ++ 03和C ++ 98。

For the sake of uniqueness, let's take the standard to be C99 and the compiler to be gcc. But I would be interested in answers for other compilers (icc, cl) and other standards (C1x, C89). In fact, just to annoy the C/C++ crowd, I'd even appreciate answers for C++0x, C++03, and C++98.

请注意:国际标准ISO / IEC 10967-1可能与此有关,但据我可以告诉它只是在资料性附录中提到

Note: International standard ISO/IEC 10967-1 may be relevant here, but as far as I could tell it was mentioned only in the informative annex.

推荐答案

看看的 -ftrapv -fwrapv

-ftrapv

这个选项的加法,减法,乘法运算符号溢出产生的陷阱。

This option generates traps for signed overflow on addition, subtraction, multiplication operations.

-fwrapv

这个选项指示编译器采用加法,减法和乘法的签署算术溢出包装了使用二进制补码再presentation左右。此标志使得一些优化和禁用等。此选项默认为Java前端启用,所要求的Jav​​a语言规范。

This option instructs the compiler to assume that signed arithmetic overflow of addition, subtraction and multiplication wraps around using twos-complement representation. This flag enables some optimizations and disables other. This option is enabled by default for the Java front-end, as required by the Java language specification.

这篇关于标准和编译器:用C整数溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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