decoding the runtime error (SIGFPE)

5.9k Views Asked by At

This is a problem on spoj.com (http://www.spoj.com/problems/PRIC/) .We have to check whether numbers of the sequence : ai=( a(i-1)+1234567890 ) mod 2^31 are prime or not, 1st number is 1. My code is given below (Please try to ignore the clumsiness.) based on sieve of eratosthenes .

The PROBLEM : We have to print "prime(1) or not(0)" for sequence upto i=33,333,333 , My code works perfectly fine for i( c3 in the code) values upto 8000 or so and after that (e.g c3>19000 ) it starts giving The SIGFPE error . Now i googled about the error , it has something to do with division/ mod by 0. But why is it that code works for c3 values upto 9000 but not beyond that?

1

There are 1 best solutions below

4
On

Depending on your compiler and development environment, you should read up on the concept of a Debugger. This answer has a guide to use gdb. If you are using Visual Studio, Code::Blocks or any other IDE, look up the debugging features. For instance how to set a breakpoint or step into/out of/over a function call for instance, watching or changing variables etc. (I'm mentioning these things to give you vital hints for Google search-words, wink wink nudge nudge).

EDIT:

Copy-pasted the code and saved it, compiled with gcc -g for debug symbols and -lm to link the math library, I ran it through gdb and it gave me this output:

Program received signal SIGFPE, Arithmetic exception.
0x0000000000400707 in sieve (prime=0x6626a0) at t.c:43
43        if (a%prime2[j]==0){

This tells you to look at line 43, at the if statement that uses a modulo operation. This seems to be the place you are doing the modulo zero.

Do note that line 43 in the document I got when I copy-pasted your code from Stackoverflow, may not be line 43 in your document.

EDIT2:

Hey my answer was unaccepted! - why was that :) ?