哪条线路出了问题——段错误,那是……?
我对 C 比较陌生(对 StackOverflow 也完全陌生 - 嘿伙计们!),在过去的几个小时里,这个段错误一直让我感到悲伤(Windows 机器上的 DevC++)。这只是一个简单的回文素数程序,但它确实让我很困难。我通常不像这里看起来的那样是一个新手程序员,但是......天哪。现在我记得为什么我想这么快地摆脱 C++ 并转向 Python。
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
FILE *outputFile;
char buffer[81];
char* strrev();
int bytesWritten;
char* strI = 0;
char *strrev(char str[])
{
char *p1 =NULL;
char *p2 =NULL;
if (! str || ! *str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
main()
{
int isPrime(int);
int i,j;
outputFile = fopen("DD:OUTPUT", "w");
if (outputFile == NULL)
{
printf("open error: %d/%s\n", errno, strerror(errno));
exit(99);
}
for (i=1; i<15000; i++)
{
if (isPrime(i)==1)
{
bytesWritten = sprintf(buffer,"%d is primepal!\n",i);
fwrite(buffer, 1, bytesWritten, outputFile);
}
}
fclose(outputFile);
return 0;
}
int isPrime(int myInt)
{
int loop;
for (loop = 2; loop < myInt/2+1; loop++)
sprintf(strI, "%s%d", 10, myInt);
{
if (myInt%loop==0 && (atoi(strrev(strI))-myInt)==0)
{
return 0;
}
return 1;
}
}
如果这是一个愚蠢的问题,我提前道歉,而且答案非常明显——但我已经正式达到了极限,无论答案多么合乎逻辑,我已经为同一问题编写了太长时间的代码,以至于无法解决这个问题。有任何意义。而且,段错误是可怕的野兽。提前感谢您提供的任何信息!
〜乔丹
I'm relatively new to C (and completely new to StackOverflow - hey guys!), and this segfault has been giving me no surcease of sorrow for the past few hours (DevC++ on a windows machine). It's just a simple palindrome prime program, but it's really giving me a hard time. I'm not generally a novice programmer like it seems here, but... Good god. Now I remember why I wanted to get away from C++ and to Python so quickly.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
FILE *outputFile;
char buffer[81];
char* strrev();
int bytesWritten;
char* strI = 0;
char *strrev(char str[])
{
char *p1 =NULL;
char *p2 =NULL;
if (! str || ! *str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
{
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
main()
{
int isPrime(int);
int i,j;
outputFile = fopen("DD:OUTPUT", "w");
if (outputFile == NULL)
{
printf("open error: %d/%s\n", errno, strerror(errno));
exit(99);
}
for (i=1; i<15000; i++)
{
if (isPrime(i)==1)
{
bytesWritten = sprintf(buffer,"%d is primepal!\n",i);
fwrite(buffer, 1, bytesWritten, outputFile);
}
}
fclose(outputFile);
return 0;
}
int isPrime(int myInt)
{
int loop;
for (loop = 2; loop < myInt/2+1; loop++)
sprintf(strI, "%s%d", 10, myInt);
{
if (myInt%loop==0 && (atoi(strrev(strI))-myInt)==0)
{
return 0;
}
return 1;
}
}
I apologize ahead of time if this is a dumb question, and the answer is very obvious -- but I've officially hit the limit where no matter how logical an answer, I've been coding the same problem for too long for it to make any sense. And also, segfaults are horrible beasts. Thank you ahead of time for anything you have to offer!
~ Jordan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
sprintf(strI, "%s%d", 10, myInt);
行可能会崩溃。strI
分配任何空间,它的定义为char* strI = 0;
将其设为char[64]
或合适的尺寸。其他一些问题:
int isPrime(int myInt);
放置在 main() 之前的某个位置。char *strrev(char str[]);
而不是char *strrev()
The line
sprintf(strI, "%s%d", 10, myInt);
is likely crashing.strI
, it's defined aschar* strI = 0;
Make it achar[64]
, or a suitable size.Some other issues:
int isPrime(int myInt);
somewhere before main().char *strrev(char str[]);
and notchar *strrev()
段错误不一定像您遇到的那么严重。使用调试符号编译程序(在 gcc 中添加 -g)并在 gdb 中运行。出现段错误后,在 gdb 中输入 bt 并按 Enter。它会告诉您段错误的确切行数。
Segfaults don't have to be as bad as you're experiencing. Compile the program with debugging symbols (add -g to gcc) and run it in gdb. After the segfault, type bt in gdb and press enter. It will tell you the exact line of your segfault.
您可能需要仔细检查与
for
相关的大括号的位置。这不是Python,仅靠缩进并不能解决问题。You might want to double-check where you've got that brace in relation to the
for
. This isn't python, and indentation alone doesn't cut it.