[英]decoding the runtime error (SIGFPE)
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 . 这是spoj.com( http://www.spoj.com/problems/PRIC/ )的问题。我们必须检查序列号是否为:ai =(a(i-1)+1234567890)mod 2 ^ 31是素数还是不是素数,第一个数是1。我的代码如下(基于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 (eg c3>19000 ) it starts giving The SIGFPE error . 问题:我们必须打印“ prime(1)or not(0)”,直到i = 33,333,333为止,我的代码对于i(代码中的c3)值高达8000左右(例如c3> 19000)它开始给出SIGFPE错误。 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?
现在,我在错误中搜索了一下,它与除以0的除数/模数有关。但是为什么代码对c3值高达9000起作用,但没有超出?
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
. 这个答案有使用
gdb
的指南。 If you are using Visual Studio, Code::Blocks or any other IDE, look up the debugging features. 如果您使用的是Visual Studio,Code :: Blocks或任何其他IDE,请查找调试功能。 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).
例如,如何设置断点或进入/退出/通过函数调用,例如观察或更改变量等。(我提到这些是给您有关Google搜索字词的重要提示,眨动眨动一下) 。
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: 复制粘贴代码并保存,用
gcc -g
调试符号和-lm
链接数学库,我通过gdb
运行它,并给出了以下输出:
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. 这告诉您查看第43行的使用模运算的if语句。 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. 请注意,当我从Stackoverflow复制粘贴代码时获得的文档中的第43行可能不是文档中的第43行。
EDIT2 : 编辑2 :
Hey my answer was unaccepted! 嘿,我的回答不被接受! - why was that :) ?
-那是为什么:)?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.