6

Hi I am new in here so please let me know if anything is wrong and I will try to better the next time .

I am trying to understand how underflow and overflow works in C++ .My understanding is if a variable's range is exceeded it will start from the other end of the range . Thus if minimum of short is -32768 and if we do a -1 to it the new value should be SHRT_MAX .(32767) Here is my code:

#include<iostream.h>
#include<limits.h>
#include<conio.h>
int main ( void )
{
 int testpositive =INT_MIN ;
 short testnegative = SHRT_MIN ; 
 cout<< SHRT_MIN<<"\n";
 cout << testnegative-1<<"\n";
 cout << INT_MIN << "\n";
 cout << testpositive-1 << "\n"; 
 cout<<testpositive-2;
 getch();
 return 0;
}   
3
  • 1
    Operands are promoted to int first so there's no overflow. Consider casting the result back to short to see what you are hoping for. Apr 4, 2012 at 13:05
  • Amazing.It works and now I understand .
    – Knownow
    Apr 4, 2012 at 13:43
  • Consider using #include <iostream> instead of #include <iostream.h>.
    – L. F.
    Jan 19, 2019 at 5:08

4 Answers 4

12

The exact behavior on overflow/underflow is only specified for unsigned types.

Unsigned integers shall obey the laws of arithmetic modulo 2^n where n is the number of bits in the value representation of that particular size of integer.

Source: Draft N3690 §3.9.1 sentence 4

This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.

Source: Draft N3690 Note 47 for §3.9.1

For normal signed integer types instead the C++ standard simply says than anything can happen.

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 undefined

Source: Draft N3690 §5 sentence 4

If we're talking about x86 processor (or most other modern processors) indeed the behavior is exactly what you describe and for the CPU there is no difference between a signed value or an unsigned value (there are signed and unsigned operations, but the value themselves are just bits).

Note that compilers can assume (and most modern optimizing compilers actually DO assume) that no signed integer overflow can occur in a correct program and for example in code like:

int do_something();
int do_something_else();

void foo() {
    int x = do_something();
    int y = x + 1;
    if (x < y) {
        do_something();
    } else {
        do_something_else();
    }
}

a compiler is free to skip the test and the else branch in the generated code completely because in a valid program a signed int x is always less than x+1 (as signed overflow cannot be considered valid behavior). If you replace int with unsigned int however the compiler must generate code for the test and for the else branch because for unsigned types it's possible that x > x+1.

For example clang compiles the code for foo to

foo():                                # @foo()
        push    rax
        call    do_something()
        pop     rax
        jmp     do_something()       # TAILCALL

where you can see that the ode just calls do_something twice (except for the strange handling of rax) and no mention of do_something_else is actually present. More or less the same code is generated by gcc.

5
  • 4
    The thing is modern compilers take advantage of signed overflows being undefined behavior to perform optimizations. So even if at the CPU level the behavior is defined, it is not a guarantee your program is not invoking undefined behavior.
    – ouah
    Apr 4, 2012 at 13:06
  • @ouah: Yes... added a note about assumptions compilers are free to do when analyzing the code
    – 6502
    Jul 18, 2018 at 6:06
  • Could we please have a specification link to the definition of unsigned underflow? Thanks! Dec 17, 2021 at 14:24
  • @ThomasWeller: n3690 says "Unsigned integers shall obey the laws of arithmetic modulo 2^n where n is the number of bits in the value representation of that particular size of integer" and as a note "This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type".
    – 6502
    Dec 17, 2021 at 17:17
  • @6502: thanks, answer is edited to cite and link the document. Dec 17, 2021 at 17:26
3

Signed overflows are undefined behavior in C++.

For example:

INT_MIN - 1

-INT_MIN

are expressions that invoke undefined behavior.

SHRT_MIN - 1 and -SHRT_MIN are not undefined behavior in an environment with 16-bit short and 32-bit int because with integer promotions the operand is promoted to int first. In an environment with 16-bit short and int, these expressions are also undefined behavior.

1

Typically yes. But since this is C++, and C++ is regulated by the C++ standard, you must know that overflows are undefined behavior.

Although what you stated probably applies on most platforms, it's in no way guaranteed, so don't rely on it.

1
  • 3
    signed overflows are undefined behaviour
    – mabraham
    Dec 11, 2017 at 0:22
0

The new value need not be SHRT_MAX it is undefined.

1
  • 1
    The behavior is undefined. The program could very well crash. Apr 4, 2012 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.