Ramine
12/28/2015 8:23:00 PM
Hello,
How can you test for signed int overflow..
Here is how you can do it in C and C++:
===
#include <limits.h>
int a = <something>;
int x = <something>;
if ((x > 0) && (a > INT_MAX - x)) /* `a + x` would overflow */;
if ((x < 0) && (a < INT_MIN - x)) /* `a + x` would underflow */;
/* ... same thing for subtraction, multiplication, and division */
===
But notice with me that this method in C and C++ is not
acceptable for realtime safety critical systems, because
on a more complex software for realtime safety critical systems,
you can forget to test for overflow of the integer and this can have a
bad consequence and even a catastrophe, so C++ and C are bad.
But with the Delphi mode of the FreePascal compiler you can do this:
Compile with -Cr(for range checking) and compile with -Co(for Integer
overflow checking), so even if on a more complex software for realtime
safety critical system you have forgot to test for for overflow of a
signed int or an unsigned integer , you can effectively catch the
exception of the overflow signed int or unsigned int with a Try Except
End; in the Delphi mode of FreePascal if you compile block with -Cr and
-Co, and that's better in FreePascal for realtime safety critical
systems, note also that i have just tested FreePascal with -Co and it
works for both overflow of a signed int or an unsigned int.
Thank you,
Amine Moulay Ramdane.