Detecting Integer Overflow ? 
Author Message
 Detecting Integer Overflow ?

Hello!

Well, I have a small(?) problem. At least it sounds simple, although I
doubt it is...

How do I check for an overflow when working with integers ?
(i.e. without including <math.h>)

One possibility would be to write some inline assembler statements
and read the overflow-bit directly. Unfortunately this stretegy is
obviously not portable.

What I'd like to have is a function like:

   /* sizeof(int) may be any number */
   int a,b,c;      
   int ov;         /* overflow flag */

   /* assume arbitary values (worst case) assigned to a and b */
   c = a * b;

   ov = integer_overflow();  /* non-zero if overflow, or else */

It would be nice if the function would be
  * independent from the number of bits an int consists of, and
  * could handle signed int as well as unsigned.

How must integer_overflow() look like?

Thanks go, of course, to any reply!

BTW: Please make sure that you reply to _me_ instead to comp.lang.c !
     This makes sure that news expire doesn't kill your reply...
     If you do want to reply to comp.lang.c, a carbon-copy (insert my

     also do. Thanks again!

-- Walter  

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Student of Electrical Engineering, University of Technology, Vienna, Austria
Address: Brunnerstr.6, A-3108 St.P?lten, Austria. Tel.: +43-2742-257191



Fri, 07 Jan 2000 03:00:00 GMT  
 Detecting Integer Overflow ?

Cast int c, a, b to double dc, da, db first and check for dc > INT_MAX or
dc < INT_MIN.  There is no magical integer overflow flag sitting around
that you can just tap into.  Indeed, some machines do not even have one.
In C++, you could use operator overloading to do the same thing with
cleaner syntax using operator overloading (but that opens another can of
worms with ambiguous overloads).



Quote:
> Hello!
[snip]
> How do I check for an overflow when working with integers ?
> (i.e. without including <math.h>)

[snip]
> What I'd like to have is a function like:

>    /* sizeof(int) may be any number */
>    int a,b,c;      
>    int ov;         /* overflow flag */

>    /* assume arbitary values (worst case) assigned to a and b */
>    c = a * b;

>    ov = integer_overflow();  /* non-zero if overflow, or else */

> It would be nice if the function would be
>   * independent from the number of bits an int consists of, and
>   * could handle signed int as well as unsigned.

> How must integer_overflow() look like?

[snip]
Sorry about the lack of email.  I have a ghastly fear of sending mail to
any address with the word 'stud' in it.


Fri, 07 Jan 2000 03:00:00 GMT  
 Detecting Integer Overflow ?

Quote:

> How do I check for an overflow when working with integers ?

> One possibility would be to write some inline assembler statements
> and read the overflow-bit directly. Unfortunately this stretegy is
> obviously not portable.

> What I'd like to have is a function like:

>    /* sizeof(int) may be any number */
>    int a,b,c;      
>    int ov;         /* overflow flag */

>    /* assume arbitary values (worst case) assigned to a and b */
>    c = a * b;

>    ov = integer_overflow();  /* non-zero if overflow, or else */

No way in standard C.

First, any integer overflow causes undefined behavior, so on some machines
your code would never reach the call to integer_overflow (), at least not
if there was an overflow.

Second, no compiler that I know has an (unportable) way to detect multiply
overflow after it happened.

One method that is not standard C, but will work on many systems and would
be quite fast: Test whether

   a * b == (double) a * (double) b

This will not work if your machine doesnt like integer overflows at all,
and it will not work if sizeof (int) == sizeof (double), for example if
int and double are both 64 bit.

The portable way is the hard way (modulo any errors while I type this): If
b > 0 then check if a is between INT_MIN / b and INT_MAX / b. If b < 0
etc. etc. etc. Be careful to handle the case that b < -INT_MAX.

-- For email responses, please remove the last emm from my address.



Sun, 09 Jan 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Help in detecting integer overflow

2. How to detect integer overflow?

3. Detecting integer overflow under SunOS--Info requested

4. Detecting integer math overflows?

5. Integer overflow question

6. Unsigned integer overflow

7. Does integer overflow cause undefined behavior

8. integer overflow

9. Integer overflow/malloc/signal question-THANKS!

10. Integer overflow/malloc/signal question

11. integer overflow behaviour

12. integer overflow warning?

 

 
Powered by phpBB® Forum Software