text: Check for Integer Overflow |
code: #include <stdio.h> NEW_LINE #include <limits.h> NEW_LINE #include <stdlib.h> NEW_LINE int addOvf ( int * result , int a , int b ) { if ( a > INT_MAX - b ) return -1 ; else { * result = a + b ; return 0 ; } } int main ( ) { int * res = ( int * ) malloc ( sizeof ( int ) ) ; int x = 2147483640 ; int y = 10 ; printf ( " % d " , addOvf ( res , x , y ) ) ; printf ( " % d " , * res ) ; getchar ( ) ; return 0 ; }