J
JKop
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.
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!
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.
-JKop
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.
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!
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.
-JKop