When to deal with integer overflow in C
Related question: How to detect integer overflow?
In C code, is integer overflow necessary when adding integers? It seems like pointers and indexes of arrays should be checked at all. When should you check the integrity of integers?
When numbers are added to C without an explicit name, or printed with printf, when will overflow occur?
Is there a way to automatically detect integer arithmetic overflow?
a source to share
I've heard of working with exception handling based on setjmp () - or longjmp () in C, but I think there is no native way to do it. What I usually do is just make sure that the types used are long enough to contain any additions / multiplications I need to do.
The whole point of using C, as opposed to managed languages ββlike C #, which will throw an OverflowException, is precisely the fact that no processor power is wasted in security checks. C will just rotate the counter and go from FFFFFFFF to 00000000, so you can check that (if a> b, etc.), but other than that, I might just recommend using longer types. 64 bits (long long) should suit all your needs.
The overflow won't happen when you print a number with printf, or at least I haven't heard of such a possibility. For complements, I just used the appropriate types and told the compiler how to interpret the values ββso that unnecessary casts can be avoided (for example, the literal "123" will be interpreted as 32 bits, but "123LL" will be 64 bits - just like ".1f" against " .1" ).
For array indices - you should always make sure you don't read / write your array, as C will happily mess up your data in many cases without throwing errors.
How to check the integrity of integers ... Well, whenever it can happen and you don't want it to happen :).
a source to share
The general answer is rare. If the result should be valid, but overflow instead, you should use the larger type. If the largest type is not enough, you should use the large int library.
There is no automatic, standard way of detecting this built into C. Some hardware supports it, but it is not standard. This was described in the thread you linked to.
Literal type is always defined, it is not always explicit. Here's a list of type literals. In general, literal arithmetic will overflow if you manage to overflow whatever type the compiler uses for an intermediate operation, or when the result is assigned to a lower precision type that doesn't have enough space.
a source to share
When you say "automatically detect overflow", what exactly do you mean? Overflow detection as a debugging tool, that is, is something that interrupts our program like a failed assertion? Or some kind of full operating mode that allows you to gracefully detect and handle the situation?
If you are interested in it as a debugging tool, then you should consult your compiler documentation. For example, GCC provides an option -ftrapv
that "generates hooks for signed overflow on add, subtract, multiply" (see code generation options )
a source to share