A question on catching integer overflow.

zhifeng Li z-li1 at northwestern.edu
Fri Apr 2 06:16:19 CEST 2004


Dear all:

I am wondering if there is a simple way to catch integer overflows. For 
example, on a 32-bit computer, MAX_LONG_INT = 2147483647L

int main()
{
	long int     a, b, c; 
	a = 211111111;
	b = 211111111;

	c=a+b; 
	return 0;
}

If the above simple program is compiled using g++ with option -ftrapv, during 
running time the program will receive SIGABRTsignal and aborts. 

Is there any better way to handle this problem? For instance, detect the 
overflow and then use arbitrary precision package? Certainly, checking every 
operation on integers would be too costly. Any suggestion? 

Thanks a lot. 

Zhifeng



More information about the gmp-bugs mailing list