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
-2
votes
1 answer
C program is randomly changing value of int
I'm really confused. I have a C server and it was working great, but then I added some code, and I thought it was working fine until my c program randomly started changing the value of an int to a negative value.
Basically I'm having my server…

Vishnu Murale
- 173
- 14
-2
votes
2 answers
Why the program is not throwing an exception when there is an overflow or underflow
When the maximum value of integer is exceeded (overflow) then the minimum value is assigned to it.
Ex- System.out.println(Integer.MAX_VALUE+1); Output: -2147483648
Here instead of throwing an exception, the program returns an incorrect value.
How…

Nirmalya Kar
- 511
- 4
- 9
-2
votes
1 answer
Checking multiple numbers for overflow
I have trouble with detecting overflow.
I am supposed to make a program that reads an int c , then int n pairs of numbers which should be multiplied.After the numbers are multiplied you need to check if it's integer overflow or not.If it's not…

Theâ Razvan
- 1
- 1
-2
votes
5 answers
Is this expression valid?
I've met this code:
std::string str;
std::getline(std::cin, str);
std::string sub = str.substr(str.find('.') + 1);
first reaction was - this is invalid code. But after some thoughts it seems to be not so simple. So is it valid C++ expression (has…

Slava
- 43,454
- 1
- 47
- 90
-2
votes
1 answer
In Assembly, How do i get past the -128 to 127 range?
So I have this declaration in .bss
answer resb 1
In answer, I store the results of the sum of 2 digit integers ranging from -99 to +99. When I try adding +99 and +99, the answer becomes negative. Any answer within the -128 to 127 range are…

ejandra
- 336
- 2
- 4
- 15
-3
votes
1 answer
Integer Overflow Java
I have a question in java about integer overflow.
When I search max integer and surfed integer for that binary, I got this "0111 1111 1111 1111 1111 1111 1111 1111" and also when I use the value 3, I got the binary as "0000 0000 0000 0000 0000 0000…

Tholkappiar
- 1
- 1
-3
votes
1 answer
Why does changing the graph in the main function result in an error?
I copied a Rust program from the website, and it ran well. However, when I changed the graph, it resulted in an error. What could be the reason for this?
use std::collections::HashMap as H;type I=i64;
type E=Vec<(I,I)>;fn d(g:&E)->bool{let mut…

licheng
- 115
- 5
-3
votes
1 answer
When printing a large integer, the system automatically changed the number without giving error message. What can i do?
Here's the piece of code:
int catalan(int n)
{
if (n<0){
return 0;
}
if (n<=1){
return 1;
}
int total = 0;
for(int i=0;i

Awakoto
- 13
- 1
-3
votes
3 answers
why sometimes I get overflow and sometimes not in C
I have this code, and the two calculations give me different results.
The first does an overflow and the second just stays with 2^31-1.
I can't figure out why.
int n4, n5;
n4 = pow(2, 31);
n4 = n4 + n4;
n5 = pow(2, 31) + pow(2, 31);
printf("\nn4:…

eilon toledano
- 113
- 5
-3
votes
1 answer
Integer overflow happening with unsigned variables
I was trying to do a factorial of a 100 on C and integer overflow happened, but what I didn't understand was why my unsigned variable became negative.
int main(void)
{
unsigned long value =1;
for(unsigned long n = 0;n<100;n++){
…

Ragoniis
- 29
- 3
-3
votes
3 answers
Avoiding overflow working modulo p
As part of a university assignment, I have to implement in C scalar multiplication on an elliptic curve modulo p = 2^255 - 19. Since all computations are made modulo p, it seems enough to work with the primitive type (unsigned long).
However, if a…

Raf
- 9
- 4
-3
votes
1 answer
Implementing / enforcing wraparound arithmetic in C
The C standard says that overflow in arithmetic is undefined.
I would like to know how to implement wraparound arithmetic in a performance-friendly way. This means that overflow checking solutions like presented here are not an option (as they slow…

Norswap
- 11,740
- 12
- 47
- 60
-3
votes
2 answers
how to avoid wrapping of input data in c using gcc?
#include
void main()
{
unsigned int a;
printf("Enter a number:");
scanf("%u",&a);
if ( a <= 4294967295) {
printf("Entered no is within limit\n");
}
else {
printf("Entered no is not in the limit");
…

pradeep
- 1
- 3
-3
votes
3 answers
First digit of the product of two very large numbers
There are 2 large integers, x and y such that z=x*y overflows.
I would like to compute the first digit of x. Directly doing so isn't possible as the result overflows.
I was wondering if there was a certain technique for this.
Example:…

Ramit Sawhney
- 31
- 1
- 9
-3
votes
2 answers
Overflow on Multiplication in C++
In my code I am trying to multiply two numbers. The algorithm is simple as (k)*(k-1)^n. I stored the product (k-1)^n in variable p1 and then I multiply it with k. For n=10, k=10 (k-1)^n-1 should be 387420489 and I got this in variable p1 but on…

satyajeet jha
- 163
- 3
- 12