[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.programming

Here is a third proof that C++ and C are bad...

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.












1 Answer

Ramine

12/28/2015 8:42:00 PM

0


Hello,


I have just tested -Co compiler option of the FreePascal
compiler, it will throw an exception that you can catch
from the software for both overflow and underflow.


Thank you,
Amine Moulay Ramdane.