哪条线路出了问题——段错误,那是……?

发布于 2024-12-08 07:50:50 字数 2911 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

渔村楼浪 2024-12-15 07:50:50

sprintf(strI, "%s%d", 10, myInt); 行可能会崩溃。

  • 您尚未为 strI 分配任何空间,它的定义为 char* strI = 0; 将其设为 char[64] 或合适的尺寸。
  • 您向 sprintf 提供了错误的参数, "%s%d" 表示第一个参数应该是字符串 ("%s") ,但您给它一个 int 。将 %s 更改为 %d

其他一些问题:

  • 不要使用 *p1 ^= *p2; hack 来交换变量,在很多情况下这不起作用。使用临时变量正确执行此操作。
  • main() 调用 isPrime(),但当时 isPrime 还没有原型。将 int isPrime(int myInt); 放置在 main() 之前的某个位置。
  • strrev 函数的原型应该是 char *strrev(char str[]); 而不是 char *strrev()

The line sprintf(strI, "%s%d", 10, myInt); is likely crashing.

  • You have not allocated any space for strI, it's defined as char* strI = 0; Make it a char[64] , or a suitable size.
  • You're giving the wrong arguments to sprintf, "%s%d" says the first parameter should be a string ("%s") , but you give it an int. Change %s to %d

Some other issues:

  • Don't use *p1 ^= *p2; hack to to swap variables, there's many cases where this does not work. Do it properly with a temp variable.
  • main() calls isPrime(), but there's no prototype for isPrime at that time. Place int isPrime(int myInt); somewhere before main().
  • The prototype for your strrev function should be char *strrev(char str[]); and not char *strrev()
烟燃烟灭 2024-12-15 07:50:50

段错误不一定像您遇到的那么严重。使用调试符号编译程序(在 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.

黑白记忆 2024-12-15 07:50:50
for (loop = 2; loop < myInt/2+1; loop++)
  sprintf(strI, "%s%d", 10, myInt);           
  {
    if (myInt%loop==0 && (atoi(strrev(strI))-myInt)==0)

您可能需要仔细检查与 for 相关的大括号的位置。这不是Python,仅靠缩进并不能解决问题。

for (loop = 2; loop < myInt/2+1; loop++)
  sprintf(strI, "%s%d", 10, myInt);           
  {
    if (myInt%loop==0 && (atoi(strrev(strI))-myInt)==0)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文