By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,883 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,883 IT Pros & Developers. It's quick & easy.

Unsigned integer overflow detection

P: n/a
Source:
http://moryton.blogspot.com/2007/08/...flow-when.html

Example from source:

char unsigned augend (255);
char unsigned const addend (255);
char unsigned const sum (augend + addend);

if (sum < augend)
{
std::puts ("Overflowed!");
}

sum = augend + addend
sum = 255 + 255
sum = 510 modulo 256 // Behind the scenes.
sum = 254

Does it touch any implementation defined or undefined behaviour, or was
that specific to signed integers (on some platforms)?

What other methods are there for detecting unsigned integer overflow
and/or underflow in C++?
Aug 20 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Raymond wrote:
Source:
http://moryton.blogspot.com/2007/08/...flow-when.html

Example from source:

char unsigned augend (255);
char unsigned const addend (255);
char unsigned const sum (augend + addend);

if (sum < augend)
{
std::puts ("Overflowed!");
}

sum = augend + addend
sum = 255 + 255
sum = 510 modulo 256 // Behind the scenes.
sum = 254

Does it touch any implementation defined or undefined behaviour, or
was that specific to signed integers (on some platforms)?
No, the behaviour is well-defined. All arithmetic operations with
unsigned values work modulo 2^N, where N is the number of bits in
the representation of the number.
>
What other methods are there for detecting unsigned integer overflow
and/or underflow in C++?
If you look in the archives (use Google Groups interface, e.g.), you
should find that unsigned does not overflow. There is no such thing
as "underflow" AFA integers are concerned, IIUIC.

There is no way to "detect" it in the current language. There is only
a way to "predict" it:

unsigned a = UINT_MAX - 2, b = UINT_MAX - 3; // or whatever
if (UINT_MAX - a < b)
std::cout << "Adding " << a << " to " << b
<< " would \"overflow\"\n";

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 20 '07 #2

P: n/a
On Aug 20, 10:24 pm, "Alf P. Steinbach" <al...@start.nowrote:
* Neelesh Bodas:
There are no overflows possible for unsigned integer arithmetic. The
question of "underflow" doesnot arise since it is "unsigned"
arithmetic.

Just a nit, but at least as far as the terminology I've been exposed to
(for some decades), "underflow" is strictly a non-integer representation
phenomenon. Integers just overflow, whether that's towards positive or
negative infinity. I.e., "overflow" and "underflow" refer to the
absolute value, not the sign.
Agreed. (But had to refer to wikipedia for that !!). so that would
make a very small change in my reply:
The question of "underflow" doesnot arise since it is unsigned
"integer" arithmetic

(Just a slip-of-quote) :)
-N

Aug 20 '07 #3

P: n/a
Alf P. Steinbach wrote:
* Raymond:
>Source:
http://moryton.blogspot.com/2007/08/...flow-when.html

Does it touch any implementation defined or undefined behaviour, or
was that specific to signed integers (on some platforms)?

In C++ unsigned integer arithmetic is defined as modulo 2^n, where n is
the number of bits.
Yes, correct, but in my haste I left out *why* I felt unsure/asked these
questions, and I think I found the part that I had read before; section
1.9 15, where overflows triggering exceptions are mentioned with regards
to integers, or signed integers if the example is to be taken literally.
If you want range checking you can check if your compiler provides range
checking for signed integer types, or you can implement a range-checked
integer type as a class, like

Quite possibly there are existing such classes freely available on the
net -- Google (and please report results of that search here! :-) ).
There is only so much time one can spend searching before beginning to
think again..

Anyway, thanks, but I think you made this a lot more complex than it
needed to be, at least for me. Existing classes? Perhaps, but for such
a small problem, that doesn't sound like the solution for me.

It wasn't easy for me to find something concrete on this subject, and I
tried finding other solutions after this one. The paper on Stroustrup's
page, also linked to from the article, only dealt with signed integers,
as your example did.
Aug 20 '07 #4

P: n/a
Victor Bazarov wrote:
Raymond wrote:
>Does it touch any implementation defined or undefined behaviour, or
was that specific to signed integers (on some platforms)?

No, the behaviour is well-defined. All arithmetic operations with
unsigned values work modulo 2^N, where N is the number of bits in
the representation of the number.
Yes.
>What other methods are there for detecting unsigned integer overflow
and/or underflow in C++?

If you look in the archives (use Google Groups interface, e.g.), you
should find that unsigned does not overflow. There is no such thing
as "underflow" AFA integers are concerned, IIUIC.
My current point of view on this; an overflow comes from adding too
much; an "underflow" comes from subtracting too much. An overflow in the
general sense does not explain whether too much was added or subtracted,
just that information was lost; it doesn't include how it happened.
There is no way to "detect" it in the current language. There is only
a way to "predict" it:

unsigned a = UINT_MAX - 2, b = UINT_MAX - 3; // or whatever
if (UINT_MAX - a < b)
std::cout << "Adding " << a << " to " << b
<< " would \"overflow\"\n";
What do you mean? The source code I linked to detects it fine, and it
appears to be fully portable. No need to think about the underlying
binary interpretation of signed integers, or exceptions either apparently.
Aug 20 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.