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
9
votes
2 answers

What is the result of 'wrapping' a multiplication overflow?

The bcache source here contains the following line: schedule_delayed_work(&dc->writeback_rate_update, dc->writeback_rate_update_seconds * HZ); writeback_rate_update_seconds is defined as unsigned int, which appears to be 32 bit on x86_64, and I…
user6854914
9
votes
1 answer

Unexpected 32-bit integer overflow in pandas/numpy int64 (python 3.6)

Let me start with the example code: import numpy from pandas import DataFrame a = DataFrame({"nums": [2233, -23160, -43608]}) a.nums = numpy.int64(a.nums) print(a.nums ** 2) print((a.nums ** 2).sum()) On my local machine, and other devs'…
Sean Kramer
  • 133
  • 7
9
votes
2 answers

C++ literal integer type

Do literal expressions have types too ? long long int a = 2147483647+1 ; long long int b = 2147483648+1 ; std::cout << a << ',' << b ; // -2147483648,2147483649
Abhinav Vishak
  • 361
  • 1
  • 5
  • 17
9
votes
7 answers

Allowing signed integer overflows in C/C++

I want signed integers to overflow when they become too big. How do I achieve that without using the next biggest datatype (or when I am already at int128_t)? For example using 8bit integers 19*12 is commonly 260, but I want the result 1 11 10 01 00…
Henry B.
  • 91
  • 1
  • 2
9
votes
3 answers

"-ftrapv" and "-fwrapv": Which is better for efficiency?

From GNU's website: -ftrapv This option generates traps for signed overflow on addition, subtraction, multiplication operations. -fwrapv This option instructs the compiler to assume that signed arithmetic overflow of addition, subtraction and…
Dovahkiin
  • 946
  • 14
  • 25
9
votes
3 answers

Using the builtin function __builtin_add_overflow_p in gcc

I was wondering on how to use this function, because I get an error when I do this: #define INT_ADD_OVERFLOW_P(a, b) \ __builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0); #include #include __int main() { int x1 =…
Eagle
  • 339
  • 1
  • 3
  • 14
9
votes
3 answers

How to avoid integer overflow?

In the following C++ code, 32767 + 1 = -32768. #include int main(){ short var = 32767; var++; std::cout << var; std::cin.get(); } Is there any way to just leave "var" as 32767, without errors?
noryb009
  • 548
  • 2
  • 10
  • 20
9
votes
3 answers

Overflow of an enum type in C?

If I have an enum type, like: enum week{ sunday=0, monday, tuesday, wednesday, thursday, friday, saturday}; and I have: enum week day; day = saturday; day++; What will be the value of day?
jnd
  • 754
  • 9
  • 20
9
votes
4 answers

Saturated addition of two signed Java 'long' values

How can one add two long values in Java so that if the result overflows then it is clamped to the range Long.MIN_VALUE..Long.MAX_VALUE? For adding ints one can perform the arithmetic in long precision and cast the result back to an int, e.g.: int…
finnw
  • 47,861
  • 24
  • 143
  • 221
9
votes
5 answers

Best way to handle and report memory allocation errors due to integer overflow in Objective-C?

To begin with, let me say that I understand how and why the problem I'm describing can happen. I was a Computer Science major, and I understand overflow/underflow and signed/unsigned arithmetic. (For those unfamiliar with the topic, Apple's Secure…
Quinn Taylor
  • 44,553
  • 16
  • 113
  • 131
9
votes
6 answers

overflows in size_t additions

I like to have my code warning free for VS.NET and GCC, and I like to have my code 64-bit ready. Today I wrote a little module that deals with in memory buffers and provides access to the data via a file-style interface (e.g. you can read bytes,…
Nils Pipenbrinck
  • 83,631
  • 31
  • 151
  • 221
9
votes
3 answers

Why abs(intmin) ~= -intmin in matlab

EDU>> intmin ans = -2147483648 EDU>> abs(intmin) ans = 2147483647 How is this possible? There must be some sort of overflow or the definitions of these functions are mingling in strange ways.
Cactus BAMF
  • 333
  • 3
  • 10
8
votes
4 answers

Bug in quicksort example (K&R C book)?

This quicksort is supposed to sort "v[left]...v[right] into increasing order"; copied (without comments) from The C Programming Language by K&R (Second Edition): void qsort(int v[], int left, int right) { int i, last; void swap(int v[], int…
functionptr
  • 317
  • 4
  • 10
8
votes
3 answers

How do I fix wrong numbers produced by integer overflow?

I had a bug that caused an integer overflow, resulting in wrong (negative) timestamps being written to the database. The code is fixed already, but I want to fix the wrong data, too. I thought, I could just take the wrong results and add…
Hanno Fietz
  • 30,799
  • 47
  • 148
  • 234
8
votes
1 answer

Is overflow of an unsigned bit field guaranteed to wrap-around?

Details The reference for bit fields at cppreference presents the following example: #include struct S { // three-bit unsigned field, // allowed values are 0...7 unsigned int b : 3; }; int main() { S s = {7}; ++s.b; //…
dfrib
  • 70,367
  • 12
  • 127
  • 192