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
13
votes
6 answers

Absolute value of INT_MIN

How could I extract the absolute value of INT_MIN without overflowing? See this code for the problem: #include #include #include int main(void) { printf("INT_MAX: %d\n", INT_MAX); printf("INT_MIN: %d\n",…
Morgan Wilde
  • 16,795
  • 10
  • 53
  • 99
12
votes
11 answers

C++ Template for safe integer casts

I am trying to write a C++ template function that will throw a runtime exception on integer overflow in casts between different integral types, with different widths, and possible signed/unsigned mismatch. For these purposes I'm not concerned with…
JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
12
votes
4 answers

No useful and reliable way to detect integer overflow in C/C++?

No, this is not a duplicate of How to detect integer overflow?. The issue is the same but the question is different. The gcc compiler can optimize away an overflow check (with -O2), for example: int a, b; b = abs(a); // will…
A Fog
  • 4,360
  • 1
  • 30
  • 32
12
votes
2 answers

get unsigned long long addition carry

I want to get the carry bit of adding two unsigned 64-bit integers in c. I can use x86-64 asm if needed. code: #include typedef unsigned long long llu; int main(void){ llu a = -1, b = -1; int carry = /*carry of a+b*/; llu res =…
neo5003
  • 131
  • 1
  • 7
12
votes
4 answers

what's the difference between mid=(beg+end)/2 and mid=beg+(end-beg)/2 in binary search?

It is a problem from C++ primer fifth edition problem 3.26, I don't know the difference between them ? May be the second one can avoid overflow.
Pegasus
  • 1,398
  • 15
  • 20
12
votes
7 answers

Explanation of the safe average of two numbers

Whenever I need to average two numbers for an algorithm like binary search, I always do something like this: int mid = low + ((high - low) / 2); I recently saw another way to do it in this post, but I don't understand it. It says you can do this in…
gsgx
  • 12,020
  • 25
  • 98
  • 149
12
votes
1 answer

long long is 8 bytes, but I get integer overflow?

Suppose long long b = 5*1024*1024*1024; // 5 gigs, small enough for 64 bits printf ("%lu\n",sizeof(long long)); // prints 8 (bytes) = 64 bits but the compiler complains: warning: integer overflow in expression [-Woverflow] Why does it…
Dervin Thunk
  • 19,515
  • 28
  • 127
  • 217
12
votes
3 answers

How to check if a number overflows an 'int'

Possible Duplicate: Best way to detect integer overflow in C/C++ I was asked this question in an interview : "Convert a string representation of a number into an integer". But if the number exceeds the max value that can be stored in a 32 bit…
AlgoMan
  • 131
  • 1
  • 1
  • 4
12
votes
3 answers

Clojure - Calculate with big numbers

I want to calculate !1000 in clojure, how can I do this without getting a integer-overflow exception? My factorial code is right now: (reduce * (range 1 1001)).
Sawny
  • 1,404
  • 2
  • 14
  • 31
11
votes
3 answers

executing a process with argc=0

Is it possible to execute a process whose argc = 0? I need to execute a program but it is extremely important for its argc to be equal to 0. Is there a way to do that? I tried to put 2^32 arguments in the command line so that it appears as if argc =…
Keeto
  • 4,074
  • 9
  • 35
  • 58
11
votes
4 answers

How to implement wrapping signed int addition in C

This is a complete rewrite of the question. Hopefully it is clearer now. I want to implement in C a function that performs addition of signed ints with wrapping in case of overflow. I want to target mainly the x86-64 architecture, but of course the…
Federico
  • 582
  • 3
  • 12
11
votes
1 answer

how to add a negative i32 number to an usize variable?

Such as: let mut a : usize = 0xFF; a += -1; // -1 may be from other variable, so there can't be a -= 1; println!("{}", a); The output is: error[E0277]: the trait bound `usize: std::ops::Neg` is not satisfied anyway?
Excosy S.P.
  • 125
  • 1
  • 8
11
votes
2 answers

Does C++ have an equivalent to boost::numeric_cast(SourceType)?

I am doing a bunch of applied-mathematics/signal-processing/algorithms C++ code. I have enabled the -Wconversion compiler warning to catch issues like runtime conversion of numbers that are type double to type int32_t. Obviously I am always…
Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177
11
votes
1 answer

SAT solving with more than 2^32 clauses

I'm trying to solve a big CNF formula using a SAT solver. The formula (in DIMACS format) has 4,697,898,048 = 2^32 + 402,930,752 clauses, and all SAT solvers I could find are having trouble with it: (P)lingeling reports that there are "too many…
Dominik Peters
  • 230
  • 1
  • 8
10
votes
5 answers

Simulating integer overflow in Python

Python 2 has two integer datatypes int and long, and automatically converts between them as necessary, especially in order to avoid integer overflow. I am simulating a C function in Python and am wondering if there are standard ways to re-enable…
brannerchinese
  • 1,909
  • 5
  • 24
  • 40