Integer overflow occurs when the result of an operation is larger than the maximal value that can be represented by the underlying integer type.
Questions tagged [integer-overflow]
1056 questions
-3
votes
2 answers
Point out potential overflow bugs
Here's my solution to interviewbit problem.
link
You are given a read only array of n integers from 1 to n.
Each integer appears exactly once except A which appears twice and B which is missing.
Return A and B.
Note: Your algorithm should…

Dmitry S.
- 23
- 3
- 8
-3
votes
4 answers
Modular exponentiation function returns the wrong int value
I got a modular exponentiation function in C which looks like this.
int modexp(int m, int e, int n)
{
printf("%i\n", m);
printf("%i\n", e);
printf("%i\n", n);
printf("\n");
if(e == 0)
{
return 1;
}
if(e%2 ==…

tpei
- 671
- 9
- 26
-4
votes
1 answer
Why is long 2500000000 overflowing?
My understanding is that a long in java has a max value of 2^63-1. Any ideas why when storing 50000^2=2500000000 in a long I get -1794967296 considering 2500000000 is less than 2^63-1? Thanks
long l = 50000 * 50000;
System.out.println(l);
I…

yonSon
- 3
- 4
-4
votes
1 answer
does integer overflow affects other variable?
#include
using namespace std;
int main() {
unsigned char char_values[2] = {0, 255};
char_values[1] += 1;
cout << (int)char_values[0] << endl;
cout << (int)char_values[1] << endl;
return 0;
}
On this code, I…

Users2530
- 9
- 3
-5
votes
2 answers
Why does GCC not detect overflow on variable initialization?
Why is it compiled without errors? What am I doing wrong?
#include
int main (){
int n1 = 90, n2 = 93, n3 = 95;
int i = 2147483647;
int ii = 2147483646;
int iii = 2147483650;
char c1[50] = {'\0'};
char c2[50] =…

akaCroc
- 83
- 4
-5
votes
3 answers
What determines the result of overflowed operations?
Example:
int max = a > b ? a : b;
int min = a + b - max;
What determines whether this will work? The processor? The hardware? The language? Help me understand this at as deep a level as possible.

user5648283
- 5,913
- 4
- 22
- 32