Questions tagged [integer-overflow]

Integer overflow occurs when the result of an operation is larger than the maximal value that can be represented by the underlying integer type.

1056 questions
86
votes
12 answers

At what point in the loop does integer overflow become undefined behavior?

This is an example to illustrate my question which involves some much more complicated code that I can't post here. #include int main() { int a = 0; for (int i = 0; i < 3; i++) { printf("Hello\n"); a = a +…
jcoder
  • 29,554
  • 19
  • 87
  • 130
85
votes
9 answers

Why Use Integer Instead of Long?

I often see questions relating to Overflow errors with vba. My question is why use the integer variable declaration instead of just defining all numerical variables (excluding double etc.) as long? Unless you're performing an operation like in a for…
Gareth
  • 5,140
  • 5
  • 42
  • 73
84
votes
4 answers

Program behaving strangely on online IDEs

I have come across the below C++ program (source): #include int main() { for (int i = 0; i < 300; i++) std::cout << i << " " << i * 12345678 << std::endl; } It looks like a simple program and gives the correct output on my…
arpanmangal
  • 1,770
  • 1
  • 17
  • 34
80
votes
6 answers

No overflow exception for int in C#?

I had this weird experience with problem number 10 on Project Euler (great site by the way). The assignment was to calculate the sum of all the prime numbers below two million. I used an int for the sum, and my algorith produced an answer, but when…
erikric
  • 4,049
  • 8
  • 32
  • 44
79
votes
14 answers

If a 32-bit integer overflows, can we use a 40-bit structure instead of a 64-bit long one?

If, say, a 32-bit integer is overflowing, instead of upgrading int to long, can we make use of some 40-bit type if we need a range only within 240, so that we save 24 (64-40) bits for every integer? If so, how? I have to deal with billions and space…
Aragon
  • 1,551
  • 1
  • 15
  • 25
77
votes
6 answers

What happens when auto_increment on integer column reaches the max_value in databases?

I am implementing a database application and I will use both JavaDB and MySQL as database. I have an ID column in my tables that has integer as type and I use the databases auto_increment-function for the value. But what happens when I get more than…
Jonas
  • 121,568
  • 97
  • 310
  • 388
75
votes
4 answers

why does long long 2147483647 + 1 = -2147483648?

Why doesn't this code print same number? : long long a, b; a = 2147483647 + 1; b = 2147483648; printf("%lld\n", a); printf("%lld\n", b); I know that int variable's maximum number is 2147483647 because int variable is 4 byte. But as I know, long…
Hoseong Jeon
  • 1,240
  • 2
  • 12
  • 20
75
votes
4 answers

Can argc overflow?

I was wandering in SO and saw this question. Then I started wondering if I can overflow argc. Standard says that argv[argc] must be a null pointer but this will be false if argc overflow. (I wrote a small C program and a python script to test it but…
Bora M. Alper
  • 3,538
  • 1
  • 24
  • 35
70
votes
5 answers

Is this a JVM bug or "expected behavior"?

I noticed some unexpected behavior (unexpected relative to my personal expectations), and I'm wondering if something if there is a bug in the JVM or if perhaps this is a fringe case where I don't understand some of the details of what exactly is…
Michael McGowan
  • 6,528
  • 8
  • 42
  • 70
64
votes
4 answers

Compiler optimizations may cause integer overflow. Is that okay?

I have an int x. For simplicity, say ints occupy the range -2^31 to 2^31-1. I want to compute 2*x-1. I allow x to be any value 0 <= x <= 2^30. If I compute 2*(2^30), I get 2^31, which is an integer overflow. One solution is to compute 2*(x-1)+1.…
59
votes
4 answers

What does BigInteger having no limit mean?

I looked into this stackoverflow question relating to Big Integer and specifically I do not understand this line (the words in italics): In the BigInteger class, I have no limits and there are some helpful functions there but it is pretty…
Geek
  • 26,489
  • 43
  • 149
  • 227
56
votes
10 answers

Can XOR of two integers go out of bounds?

I had been studying the algorithm for finding lonely integers in an array, and here is the implementation: int arr[] = {10, 20, 30, 5, 20, 10, 30}; int LonelyInteger = 0; for(int i=0; i< 7; i++) { LonelyInteger = LonelyInteger ^ arr[i]; } The…
Expert Novice
  • 1,943
  • 4
  • 22
  • 47
51
votes
6 answers

Does integer overflow cause undefined behavior because of memory corruption?

I recently read that signed integer overflow in C and C++ causes undefined behavior: If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is…
Vinz
  • 3,030
  • 4
  • 31
  • 52
51
votes
9 answers

Why don't languages raise errors on integer overflow by default?

In several modern programming languages (including C++, Java, and C#), the language allows integer overflow to occur at runtime without raising any kind of error condition. For example, consider this (contrived) C# method, which does not account for…
Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
48
votes
5 answers

-1 * int.MinValue == int.MinValue?? Is this a bug?

In C# I see that -1 * int.MinValue == int.MinValue Is this a bug? It really screwed me up when I was trying to implement a search tree. I ended up using (int.MinValue + 1) so that I could properly negate it.
James
  • 1,085
  • 9
  • 16
1
2
3
70 71