What Happens When I Assign a Number Larger Than Int_Max to an Int

What happens when I assign a number larger than INT_MAX to an int?

It is implementation-defined behaviour. This means that your compiler must provide documentation saying what happens in this scenario.

So, consult that documentation to get your answer.

A common way that implementations define it is to truncate the input integer to the number of bits of int (after reinterpreting unsigned as signed if necessary).

C++14 Standard references: [expr.ass]/3, [conv.integral]/3

Strange behaviour when intentionally assigning `i` value greater than INT_MAX

  1. Yes, signed integer overflow has undefined behavior in C.

  2. i > INT_MAX is never true for any int i. How can you have an int greater than the maximum possible int?

Storing number larger than INT_MAX in an int

The stream extraction operator >> notes that you can't stream in that large number and sets the fail bit, and does not modify the value of x.

Subsequent reading from that input stream (cin in this case) fail immediately, and thus the value of x remains unchanged (and in this case, undefined).

c++: int overflows when it is smaller than INT_MAX

In your loop, the expression 1 + (N + 1 - i) * i (which is evaluated as a long long int) quickly and frequently becomes greater than INT_MAX, and this causes integer overflow when you try to add it to the (int) result.

Adding a quick 'check' to your code, as shown below, will demonstrate this:

int main()
{
cin >> N >> K;
long long int result = 0;
int IntResult = 0;
int count = 0;
for (long long int i = K; i <= N + 1; i++) {
// Check for intermediate overflow potential...
long long int lli = 1LL + (N + 1LL - i) * i;
if (lli > INT_MAX) cout << lli << " (" << ++count << ")" << endl;
result += 1 + (N + 1 - i) * i;
IntResult += 1 + (N + 1 - i) * i;
}
cout << result % (1000000000 + 7) << endl;
cout << IntResult % (1000000000 + 7) << endl;
return 0;
}

When I run this code on my platform (32-bit int and 64-bit long long int) with your given inputs (141421 and 35623), the "overflow check" line actually happens 88,498 times! Here are the last three lines of output:

...
2147524241 (88498)
220280457
619089693

Furthermore, even though the above-checked addend may not, in itself, overflow an int, the result of adding it to the existing (potentially non-zero) value in result could still do so. Indeed, changing the 'check' line to this:

if ((lli + result) > INT_MAX) cout << lli << " (" << ++count << ")"  << endl;

shows that the loop overflows on every iteration!

Difference of two int pointer that is greater than INT_MAX

Is is possible that the difference of two int* pointers on the elements of the same array is greater than INT_MAX?

Yes this is possible. An array can be larger that INT_MAX elements. Such large arrays are infrequently used.

C provides ptrdiff_t as the integer type for pointer subtraction.

int *pointer_a;
int *pointer_b;
...
// pointer_a, pointer_b point to elements in the same array object.
ptrdiff_t diff = pointer_a - pointer_b;

// Use `%td` to print a `ptrdiff_t`.
printf("diff %td\n", diff);

When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the two array elements. The size of the result is implementation-defined, and its type (a signed integer type) is ptrdiff_t defined in the <stddef.h> ... C11dr §6.5.6 9


how could I access the elements of such an array that are beyond INT_MAX?

To index elements outside the int range, use a type that is specified to work.

size_t is some unsigned type specified to be wide enough to index all the array.

// some big array, maybe in global memory.
int big[10000000000];

....
size_t index = foo();
printf("big[%zu] = %d\n", index, big[index]);

Infinite loop when trying to read an int greater than INT_MAX

The problem isn't the integer representation. It's that the conversion of the input fails, which means it will still be left in the input buffer, leading to your infinite loop reading the exact same data over and over again.

You need to check if the input succeeded or not, and if it failed you need to clear the buffer as well as the error flags:

if (!(cin >> num))
{
// Error, failed to parse the input

// First clear the error status flags
cin.clear();

// Then remove the current line of input from the buffer
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
else
{
// Input okay, use it...
}

Since it's a lot to write for each input, I suggest you move it out to a function that you call to get input.



Related Topics



Leave a reply



Submit