Re: signed int overflow

From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 09/20/04


Date: Mon, 20 Sep 2004 11:10:08 +0200

JKop wrote:

> You know how the saying goes that *unsigned* overflow is...
> well.. defined. That means that if you add 1 to its maximum
> value, then you know exactly what value it will have
> afterward on all implementations.

Actually, the overflow behavior is always undefined, in theory even for
unsigned integers. But that could never happen because the standard says
that unsigned integers don't overflow at all. The wrap-around is just part
of the normal unsigned integer behavior and not seen as overflow:
(2^n here of course means 2 raised to the power of n)

3.9.1 Fundamental types

...

4 Unsigned integers, declared /unsigned/, 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. 41)

...
41) 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.

> But then you have signed integers. Let's say a signed
> integer is set to its maximum positive value. If you add 1
> to it, what happens?:
>
> A) It's implementation defined what value it will
> represent, eg. it could roll back around to 0, or it could
> roll back around to the maximum negative number.
>
> B) Undefined behaviour.
>
>
> Please say A!

"I'm sorry Dave, I'm afraid I can't do that". The answer is B.
>From the C++ standard:

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, unless such an expression is a constant expression,
in which case the program is ill-formed.

> For instance:
>
> int main()
> {
> //on a 32-Bit machine
> signed int i = 2147483648;
>
> ++i;
> }
>
> Is that just plain old undefined behaviour, eg. the machine
> can blow up and spit nitric acid in your face if it wants
> to...
>
> or is it simply benignly just implementation specific what
> value "i" will represent after the incrementation?
>
> If B is the case, it looks like I'm off to write "class
> signed_dof_int", where dof = defined overflow. I'll use a
> template for it, so you can do it with all of the integral
> types. What's the best way to figure out the maximum value
> of a particular type? I believe that the Standard Library
> contains some global const variables, stuff like MAX_INT,
> but I'd prefer a method I could use within a template.

std::numeric_limits<thetype>::max() from the <limits> header.



Relevant Pages

  • Re: time to get rid of unsigned?
    ... >> unsigned integers. ... It seems quite reasonable to blame the existence of unsigned ... > Overflow pure and simple. ... But because of the mix of signed and unsigned types it does no ...
    (comp.lang.cpp)
  • Re: time to get rid of unsigned?
    ... >> unsigned integers. ... It seems quite reasonable to blame the existence of unsigned ... > Overflow pure and simple. ... But because of the mix of signed and unsigned types it does no ...
    (comp.lang.cpp)
  • Re: Data hiding in C++
    ... |> Yes, I understand that these limits are there, but this is ... | expression is going to overflow. ... | Overflow has defined results for the unsigned integers. ... | You could write a class that implements a signed integer ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Data hiding in C++
    ... > | establish when overflow has occurred in a reliable and portable way. ... I meant 'signed integer'. ... > | execptions when overflow is detected. ... > what when you want to check unsigned integers? ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Adding Older Versions of GCC To The Tool Chain ... safely?!?!
    ... There is by definition no such thing as 'an unsigned overflow', ... the bits of the object representation ... for conversion to an unsigned type to be performed on mathematical ... If an exceptional condition occurs during the evaluation of an ...
    (comp.os.linux.development.apps)