简体   繁体   English

运行时错误(SIGSEGV)

[英]runtime error (SIGSEGV)

can anyone tell me whats wrong in the following program that accepts 1 or 2 digit integers untill it encounters the number 42 after which it prints the previously entered numbers??when i upload this to the sphere online judge site it says compilation successful but runtime error (SIGSEGV). 谁能告诉我下面接受1或2位数字整数的程序出了什么问题,直到遇到数字42之后才打印先前输入的数字?当我将其上传到Sphere在线判断网站时,它说编译成功但运行时错误(SIGSEGV)。

#include <stdio.h>
int main()
{
  int i;
  FILE *fp;
  fp=fopen("\\db.txt","w+");
  if(fp==NULL)
  {
   printf("file not exitsts and cant be created");
   system("exit");
  }
  while(1)
  {
   scanf("%d",&i);
   if(i==42)
   break;
   else
   {
    fprintf(fp,"%d\n",i); 
   }    
  }
  fclose(fp);
  fp=fopen("\\db.txt","r+");
  if(fp==NULL)
  {
   printf("file not exitsts and cant be created");
   system("exit");
  }
  fscanf(fp,"%d",&i);
  printf("%d\n",i); 
  while((!feof(fp)))
  {
    fscanf(fp,"%d",&i);
    if(!feof(fp))
    printf("%d\n",i);                
  }

  fclose(fp);
  return 0;
}

I don't know what you think: 我不知道您的想法:

system("exit");

will do, but the way to exit a program in C is: 可以,但是退出C语言程序的方法是:

exit(1);

It seems like you're trying to answer this: http://www.spoj.pl/problems/TEST/ . 似乎您正在尝试回答以下问题: http : //www.spoj.pl/problems/TEST/ This problem certainly does not require you to read or write anything from a file, and their server may not allow you to open files on its disk. 当然,此问题不需要您从文件中读取或写入任何内容,并且它们的服务器可能不允许您打开其磁盘上的文件。 Even if it does, you're trying to use a windows-style path (with a backslash) on what may be a non-Windows server. 即使这样做,您仍尝试在非Windows服务器上使用Windows样式的路径(带有反斜杠)。 And even if it does allow file creation and windows-style path separation, you are trying to create your file in the filesystem root directory, and they almost certainly do not allow file creation there . 即使它确实允许文件创建和Windows样式的路径分隔,您仍试图在文件系统根目录中创建文件,并且几乎可以肯定的是,它们不允许在此处创建文件。

Combined with the system("exit") issue that everyone pointed out where it doesn't actually exit the program, this will cause you to receive a NULL file pointer and crash when you try to use it. system("exit")问题结合在一起,每个人都指出了它实际上并未退出程序的位置,这将导致您收到NULL文件指针并在尝试使用它时崩溃。

Re-read the problem description - you're over-thinking it. 重新阅读问题描述-您想得太多了。 It doesn't say anywhere that you have to wait until you get a 42 to print out the other numbers. 它并没有说您必须等待直到得到42才能打印出其他数字。 All you have to do is print back what is entered until you get a 42. That should make the solution much simpler. 您要做的就是将输入的内容打印回去,直到得到42。这将使解决方案更加简单。 It's not supposed to be even a mildly challenging problem; 它甚至不应该是一个具有挑战性的问题。 it's just supposed to familiarize you with their system. 只是应该让您熟悉他们的系统。

You should replace 你应该更换

system("exit");

with

exit(1);

or, because you're already in main : 或者,因为您已经在main

return 1;

I suspect the SIGSEGV is caused because you cannot write to the file \\\\db.txt , but the program continues because system("exit") is not causing it to terminate. 我怀疑是SIGSEGV的原因,因为您无法写入文件\\\\db.txt ,但是该程序继续运行,因为system("exit")并未使其终止。

On an semi-related note, SIGSEGV is usually a Unix signal, and path separators on Unix are / rather than \\ . 半相关的说明,SIGSEGV通常是Unix信号,Unix上的路径分隔符是/而不是\\

I don't know precisely the cause of the SEGV, but I guess it is because the input doesn't match what you expect. 我不确切知道SEGV的原因,但我想这是因为输入与您期望的不匹配。 In any case, this line doesn't do what you think it does: 在任何情况下,此行都不会执行您认为的操作:

system("exit");
  1. at which line do you receive the error? 您在哪一行收到错误?
  2. is your empty #include intended? 您的空#include是预期的吗? i think it should mean #include 我认为这应该意味着#include
  3. have you got the error for every input or just for 42? 您是否为每个输入或42个错误?

regards 问候

SIGSEGV is an access violation error, which indicates a null pointer. SIGSEGV是访问冲突错误,指示null指针。 Since system("exit") isn't doing anything, fp is getting set to null , and then when you try to use that pointer (for example with fprintf() )... boom, your program crashes. 由于system("exit")没有执行任何操作,因此fp设置为null ,然后当您尝试使用该指针时(例如,与fprintf() )...繁荣,程序崩溃。

Replace system("exit") with return 1 (or whatever error code you desire), that should fix it. system("exit")替换为return 1 (或所需的任何错误代码),即可解决该问题。

$ gcc -Wall test.c -o test
test.c: In function ‘main’:
test.c:8: warning: implicit declaration of function ‘system’
$ ./test 
1 
2
3
10
42
1
2
3
10

无法复制

But yes, I do agree that the system("exit") does not what you expect. 但是,是的,我确实同意system("exit")与您期望的不一样。 What you are exiting from, with that call, is a subshell that is spawned by your program, and then goes on. 通过该调用,您从中退出的是一个子shell,该子shell由您的程序生成,然后继续。 From the system man page system手册页

The system() function hands the argument command to the command interpreter sh(1). system()函数将自变量命令交给命令解释器sh(1)。 The calling process waits for the shell to finish executing the command, ignoring SIGINT and SIGQUIT, and blocking SIGCHLD. 调用进程等待外壳程序完成命令执行,忽略SIGINT和SIGQUIT,并阻止SIGCHLD。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM