Related question: How to detect integer overflow?
相关问题:如何检测整数溢出?
In C code, should integer overflow be addressed whenever integers are added? It seems like pointers and array indexes should be checked at all. When should integer overflow be checked for?
在C代码中,每当添加整数时,是否应该对整数溢出进行寻址?似乎应该检查指针和数组索引。什么时候应该检查整数溢出?
When numbers are added in C without type explicitly mentioned, or printed with printf, when will overflow occur?
当在C中添加数字而没有明确提到的类型,或者用printf打印时,何时会发生溢出?
Is there a way to automatically detect integer arithmetic overflow?
有没有办法自动检测整数运算溢出?
3 个解决方案
#1
2
I've heard about setjmp()- or longjmp()-based exception handling in C, but I think there's no native way of doing this. What I usually do is just make sure the types used are long enough to contain all the additions/multiplications I'll need to make.
我听说过C中的setjmp() - 或基于longjmp()的异常处理,但我认为没有本地方法可以做到这一点。我通常做的只是确保使用的类型足够长,以包含我需要做的所有添加/乘法。
The whole point of using C, as opposed to managed languages such as C#, which will throw an OverflowException, is precisely the fact that no CPU power is wasted on safety checks. C will simply turn the counter around, and go from FFFFFFFF to 00000000, so you can check for that (if a>b and such), but other than that I can just recommend using longer types. 64 bits (long long) should address all your needs.
使用C的全部意义,与C#之类的托管语言相反,它将抛出OverflowException,这正是安全检查不会浪费CPU能力的事实。 C只需转动计数器,然后从FFFFFFFF转到00000000,这样你就可以检查(如果a> b等),但除此之外我可以建议使用更长的类型。 64位(长很长)应该满足您的所有需求。
Overflow won't occur when you print a number with printf, or at least I haven't heard of such a possibility. For additions, I'd just use adequate types and tell the compiler how to interpret the values so that you can avoid unnecessary casts (like, the literal "123" will be interpreted as 32 bit, but "123LL" will be 64 bit - same as with ".1f" vs. ".1").
使用printf打印数字时不会发生溢出,或者至少我没有听说过这种可能性。对于添加,我只使用足够的类型并告诉编译器如何解释这些值,以便您可以避免不必要的强制转换(例如,文字“123”将被解释为32位,但“123LL”将为64位 - 与“.1f”与“.1”相同。
For array indices - you should always make sure you don't read/write out of your array, as C in many cases will happily corrupt your data without causing an error.
对于数组索引 - 您应始终确保不读取/写出数组,因为在许多情况下,C会很高兴地破坏您的数据而不会导致错误。
As for when integer overflow should be checked for... Well, whenever it may occur and you don't want it to occur :).
至于什么时候应该检查整数溢出...好吧,只要它可能发生,你不希望它发生:)。
#2
1
The general answer is rarely. If the result should be valid, but overflowed instead then you should have used a larger type. If the largest type isn't sufficient then you should have used a big int library.
一般的答案很少。如果结果应该有效,但是溢出而不是那么你应该使用更大的类型。如果最大的类型不够,那么你应该使用一个大的int库。
There is no automatic, standard way to detect this built into C. Some hardware supports it, but it isn't standard. This was covered in the thread you linked to.
没有自动的,标准的方法来检测内置于C中的内容。某些硬件支持它,但它不是标准的。这与您链接的主题有关。
The type of literals is always defined, it's just not always explicit. Here's a list of literal types. Generally performing arithmetic with literals will overflow either if you manage to overflow whatever type the compiler uses for intermediate operation, or when the result gets assigned to a type of lower precision that doesn't have enough space.
文字的类型总是被定义,它并不总是明确的。这是一个文字类型列表。通常,如果您设法溢出编译器用于中间操作的任何类型,或者将结果分配给没有足够空间的较低精度类型,则通常使用文字执行算术会溢出。
#3
1
When you say "automatically detect overflow", what exactly do you mean? Overflow detection as a debugging tool, i.e. something that aborts our program is a way similar to a failed assertion? Or some kind of full-time run-time mechanism that would let you detect and handle the situation gracefully?
当你说“自动检测溢出”时,你究竟是什么意思?溢出检测作为调试工具,即中止我们的程序的方式类似于失败的断言?或者某种全职运行时机制可以让你优雅地检测和处理这种情况?
If you are interested in it as a debugging tool, then you should consult your compiler documentation. GCC, for example, provides -ftrapv
option that "generates traps for signed overflow on addition, subtraction, multiplication operations" (see code generation options)
如果您对它作为调试工具感兴趣,那么您应该查阅编译器文档。例如,GCC提供-ftrapv选项,“在加法,减法,乘法运算时生成有符号溢出的陷阱”(参见代码生成选项)
#1
2
I've heard about setjmp()- or longjmp()-based exception handling in C, but I think there's no native way of doing this. What I usually do is just make sure the types used are long enough to contain all the additions/multiplications I'll need to make.
我听说过C中的setjmp() - 或基于longjmp()的异常处理,但我认为没有本地方法可以做到这一点。我通常做的只是确保使用的类型足够长,以包含我需要做的所有添加/乘法。
The whole point of using C, as opposed to managed languages such as C#, which will throw an OverflowException, is precisely the fact that no CPU power is wasted on safety checks. C will simply turn the counter around, and go from FFFFFFFF to 00000000, so you can check for that (if a>b and such), but other than that I can just recommend using longer types. 64 bits (long long) should address all your needs.
使用C的全部意义,与C#之类的托管语言相反,它将抛出OverflowException,这正是安全检查不会浪费CPU能力的事实。 C只需转动计数器,然后从FFFFFFFF转到00000000,这样你就可以检查(如果a> b等),但除此之外我可以建议使用更长的类型。 64位(长很长)应该满足您的所有需求。
Overflow won't occur when you print a number with printf, or at least I haven't heard of such a possibility. For additions, I'd just use adequate types and tell the compiler how to interpret the values so that you can avoid unnecessary casts (like, the literal "123" will be interpreted as 32 bit, but "123LL" will be 64 bit - same as with ".1f" vs. ".1").
使用printf打印数字时不会发生溢出,或者至少我没有听说过这种可能性。对于添加,我只使用足够的类型并告诉编译器如何解释这些值,以便您可以避免不必要的强制转换(例如,文字“123”将被解释为32位,但“123LL”将为64位 - 与“.1f”与“.1”相同。
For array indices - you should always make sure you don't read/write out of your array, as C in many cases will happily corrupt your data without causing an error.
对于数组索引 - 您应始终确保不读取/写出数组,因为在许多情况下,C会很高兴地破坏您的数据而不会导致错误。
As for when integer overflow should be checked for... Well, whenever it may occur and you don't want it to occur :).
至于什么时候应该检查整数溢出...好吧,只要它可能发生,你不希望它发生:)。
#2
1
The general answer is rarely. If the result should be valid, but overflowed instead then you should have used a larger type. If the largest type isn't sufficient then you should have used a big int library.
一般的答案很少。如果结果应该有效,但是溢出而不是那么你应该使用更大的类型。如果最大的类型不够,那么你应该使用一个大的int库。
There is no automatic, standard way to detect this built into C. Some hardware supports it, but it isn't standard. This was covered in the thread you linked to.
没有自动的,标准的方法来检测内置于C中的内容。某些硬件支持它,但它不是标准的。这与您链接的主题有关。
The type of literals is always defined, it's just not always explicit. Here's a list of literal types. Generally performing arithmetic with literals will overflow either if you manage to overflow whatever type the compiler uses for intermediate operation, or when the result gets assigned to a type of lower precision that doesn't have enough space.
文字的类型总是被定义,它并不总是明确的。这是一个文字类型列表。通常,如果您设法溢出编译器用于中间操作的任何类型,或者将结果分配给没有足够空间的较低精度类型,则通常使用文字执行算术会溢出。
#3
1
When you say "automatically detect overflow", what exactly do you mean? Overflow detection as a debugging tool, i.e. something that aborts our program is a way similar to a failed assertion? Or some kind of full-time run-time mechanism that would let you detect and handle the situation gracefully?
当你说“自动检测溢出”时,你究竟是什么意思?溢出检测作为调试工具,即中止我们的程序的方式类似于失败的断言?或者某种全职运行时机制可以让你优雅地检测和处理这种情况?
If you are interested in it as a debugging tool, then you should consult your compiler documentation. GCC, for example, provides -ftrapv
option that "generates traps for signed overflow on addition, subtraction, multiplication operations" (see code generation options)
如果您对它作为调试工具感兴趣,那么您应该查阅编译器文档。例如,GCC提供-ftrapv选项,“在加法,减法,乘法运算时生成有符号溢出的陷阱”(参见代码生成选项)