What is an integer overflow error?

What is an integer overflow error? Why am I so worried about such an error? What are some ways to avoid or prevent this?

+11
language-agnostic integer-overflow
Apr 14 '10 at 21:46
source share
9 answers

Integer overflow occurs when you try to express a number that is greater than the largest number that the integer type can handle.

If you try to express the number 300 in one byte, you will have an integer overflow (maximum 255). 100,000 in two bytes is also an integer overflow (65,535 is the maximum).

You need to take care of this because the math operations will not behave as you expect. A + B is actually not equal to the sum of A and B if you have integer overflow.

You avoid this by not creating the conditions in the first place (usually either by choosing an integer type to be large enough so that you do not overflow, or by restricting user input so that overflow does not occur).

+15
Apr 14 '10 at 21:51
source share

The easiest way to explain this is with a trivial example. Imagine we have a 4-bit unsigned integer. 0 will be 0000, and 1111 will be 15. Therefore, if you increment 15 instead of 16, you exchange back to 0000, since 16 is actually 10,000, and we cannot imagine this with less than 5 bytes. Ergo overflow.

In practice, the numbers are much larger and are rounded to a large negative number when overflowing if the int is signed, but basically this happens.

Another way to look at this is to consider it basically the same as what happens when the odometer in your car rolls back to zero after hitting 999,999 km / miles.

+7
Apr 14 '10 at 21:51
source share

When you store an integer in memory, the computer saves it as a series of bytes. They can be represented as a series of ones and zeros.

For example, zero will be represented as 00000000 (8-bit integers), and often 127 will be represented as 01111111 . If you add one of 127, it will โ€œflipโ€ the bits and replace it with 10000000 , but in the standard two compliments , it is actually used to represent -128. This overflows the meaning.

The same thing happens with unsigned numbers: 255 ( 11111111 ) plus 1 will become 100000000 , but since there are only 8 "bits", it ends as 00000000 , which is 0.

You can avoid this by doing the correct range check for your correct integer size or using a language that does the correct exception handling for you.

+2
Apr 14 '10 at 21:52
source share

I would like to be a little opposed to all the other answers that somehow take crappy broken math for granted. The question is related to the agnostic language and in a huge number of languages, integers simply never overflow, so here is my kind of sarcastic answer:

What is an integer overflow error?

An obsolete artifact from dark generations of computers.

why do i need this?

No.

How can it be avoided?

Use a modern programming language in which integers do not overflow. (Lisp, Scheme, Smalltalk, Self, Ruby, Newspeak, Ioke, Haskell, take your choice ...)

+2
Apr 14 '10 at 23:17
source share

An integer overflow error occurs when an operation makes an integer value greater than the maximum.

For example, if the maximum value you can get is 100000, and your current value is 99999, then adding 2 will make it "overflow".

You have to take care of integer overflows because data can be changed or lost unintentionally and can escape it either with a large integer type (see long int in most languages) or using a scheme that converts long strings of digits to very large integers numbers.

+1
Apr 14 '10 at 21:50
source share

Overflow occurs when the result of an arithmetic operation does not match the data type of the operation. You may have an overflow with an unsigned byte if you add 255 + 1, because the result (256) does not fit into 8 bit bytes.

You may have a floating point overflow if the result of the floating point operation is too large to represent an exponential type of floating point data or mantissa.

You can also have an underflow with floating point types when the result of a floating point operation is too small to represent a floating point data type. For example, if a floating point data type can handle metrics in the range of -100 to +100, and you assign a value with a -80 metric, the result will have a metric of about -160, which will not match the given floating point data type.

You need to worry about overflows and threads in your code, because it can be a silent killer: your code produces incorrect results, but may not signal an error.

Regardless of whether you can safely ignore overflows, a lot depends on the nature of your program. Images of screen pixels from 3D data have a much larger tolerance for numerical errors than, say, financial calculations.

Overflow checking is often disabled in the default compiler settings. What for? Since additional code to check for overflow after each operation takes time and space, which can degrade the performance of your code at run time.

Do yourself a favor, and at least develop and test your code with overflow checking enabled.

+1
Apr 14 '10 at 23:52
source share

From wikipedia :

In computer programming, integer overflow occurs when the arithmetic attempts to create a numerical value that is greater than what can be represented in the storage space. For example, adding 1 to the largest value that can be represented is an integer overflow. The most common result in these cases is for the least significant represented bits of the result will be (the result ends).

You should take care of this, especially when choosing the appropriate data types for your program, or you may get very subtle errors.

0
Apr 14 2018-10-14T00:
source share

From http://www.first.org/conference/2006/papers/seacord-robert-slides.pdf :

Integer overflow occurs when the integer exceeds the maximum value or decreases beyond its minimum value. Overflow can be signed or unsigned.

PS: In PDF there is a detailed explanation of overflows and other integer error conditions, as well as ways to eliminate / prevent them.

0
Apr 14 '10 at 21:50
source share

This happens when you try to use an integer for a value that is larger than the internal structure of the integer can support due to the number of bytes used. For example, if the maximum integer size is 2,147,483,647 and you are trying to save 3,000,000,000, you will get an integer overflow error.

-one
Apr 14 '10 at 21:50
source share



All Articles