Re: signed int overflow
From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 09/20/04
- Next message: Rolf Magnus: "Re: #define vs. const"
- Previous message: Timothy Madden: "C and only C language has a standard 64 bit integer type ?"
- In reply to: JKop: "signed int overflow"
- Next in thread: JKop: "Re: signed int overflow"
- Reply: JKop: "Re: signed int overflow"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Rolf Magnus: "Re: #define vs. const"
- Previous message: Timothy Madden: "C and only C language has a standard 64 bit integer type ?"
- In reply to: JKop: "signed int overflow"
- Next in thread: JKop: "Re: signed int overflow"
- Reply: JKop: "Re: signed int overflow"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|