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

Exceeding limits during arithmetic

P: n/a
Is there any standard (or even non-standard) way to detect limit overflow in
arithmetic in C ?

eg.

/* assuming 4 byte ints.. */
int a=2147483647;
int b=2147483647;
int c;

c=a*b;


Jun 12 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On Tue, 12 Jun 2007 14:27:03 +0100, Mike Aubury
<mi*********@aubit.comwrote in comp.lang.c:
Is there any standard (or even non-standard) way to detect limit overflow in
arithmetic in C ?

eg.

/* assuming 4 byte ints.. */
int a=2147483647;
int b=2147483647;
int c;

c=a*b;
No, there is not. If the result of an arithmetic operation on signed
integer or floating point types is outside the range of the type, the
result is undefined behavior. If an arithmetic operation on unsigned
integer types overflows or underflows, the behavior is well-defined,
but no indication is possible.

If it is important, you need to check before you perform the
operation.

Here is a quick snippet for adding two positive signed ints:

#include <limits.h>

/* ... */

if ((INT_MAX - a) b)
{
c = a + b;
}
else
{
/* overflow handler */
}

/* ... */

There are various cases for addition and subtraction. For addition,
an overflow can only occur when the two values have the same sign. For
subtraction, it can only occur when they have opposite signs.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 12 '07 #2

P: n/a
"Mike Aubury" writes:
Is there any standard (or even non-standard) way to detect limit overflow
in
arithmetic in C ?

eg.

/* assuming 4 byte ints.. */
int a=2147483647;
int b=2147483647;
int c;

c=a*b;
No. Sadly, I don't know of a single high level language that makes the
overflow flag, which the hardware designer so carefully set, throw that flag
away. If I were looking for a language that did this right, I would
immediately think of Ada, but that might be just wishful thinking.
Jun 12 '07 #3

P: n/a
Jack Klein <ja*******@spamcop.netwrites:
[...]
Here is a quick snippet for adding two positive signed ints:

#include <limits.h>

/* ... */

if ((INT_MAX - a) b)
{
c = a + b;
}
else
{
/* overflow handler */
}

/* ... */
[...]

Quick, but not quite correct; consider a == -1.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 12 '07 #4

P: n/a
Mike Aubury wrote:
Is there any standard (or even non-standard) way to detect limit overflow in
arithmetic in C ?

eg.

/* assuming 4 byte ints.. */
int a=2147483647;
int b=2147483647;
int c;

c=a*b;
If you are using the lcc-win32 compiler system you can use the intrinsic
function
bool _overflow(void);

That "pseudo" function will return 1 if the overflow flag is set, zero
otherwise.

Jun 12 '07 #5

P: n/a
"osmium" <r1********@comcast.netwrites:
[...]
No. Sadly, I don't know of a single high level language that makes the
overflow flag, which the hardware designer so carefully set, throw that flag
away. If I were looking for a language that did this right, I would
immediately think of Ada, but that might be just wishful thinking.
<OT>
Yes, Ada requires overflow to be detected; numeric overflow raises an
exception, which may be handled by the program. (I think some
compilers don't enable this check by default.) I'm sure there are
other languages that have similar rules, but of course the details
are off-topic.
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 12 '07 #6

P: n/a
Keith Thompson said:
Jack Klein <ja*******@spamcop.netwrites:
[...]
>Here is a quick snippet for adding two positive signed ints:
<snip>
Quick, but not quite correct; consider a == -1.
Read up. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 12 '07 #7

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
Keith Thompson said:
>Jack Klein <ja*******@spamcop.netwrites:
[...]
>>Here is a quick snippet for adding two positive signed ints:

<snip>
>Quick, but not quite correct; consider a == -1.

Read up. :-)
D'oh!

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 12 '07 #8

P: n/a
jacob navia wrote:
Mike Aubury wrote:
>Is there any standard (or even non-standard) way to detect limit
overflow in arithmetic in C ?
.... snip ...
>
If you are using the lcc-win32 compiler system you can use the
intrinsic function
bool _overflow(void);

That "pseudo" function will return 1 if the overflow flag is set,
zero otherwise.
Perfectly good idea, but non-standard. I am glad you are putting
it in the implementation namespace.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 12 '07 #9

P: n/a
Mike Aubury wrote:
>
Is there any standard (or even non-standard) way to detect limit
overflow in arithmetic in C ?

eg.
/* assuming 4 byte ints.. */
int a=2147483647;
int b=2147483647;
int c;

c=a*b;
if (INT_MAX / b < a) overflow();
else c = a * b;

and #include <limits.h>. Diddle to handle -ve input values.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 13 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.