C语言 如何立即关闭 C 中的程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7973583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I immediately close a program in C?
提问by Syntax_Error
I am writing C code, in which I am analyzing some data. I have set the program to handle only 100 data inputs. When it has more than 100 inputs, it is giving a segmentation fault. I want to create a way so that when the number of inputs is above 100 the user will be warned and the program will terminate. I know how to do it from the main function by simply return 0, however I am multiple function calls away from main and it is difficult to do that even a return 0in this function will keep it looping.
我正在编写 C 代码,其中我正在分析一些数据。我已将程序设置为仅处理 100 个数据输入。当它有超过 100 个输入时,就会出现分段错误。我想创建一种方法,以便当输入数量超过 100 时,用户将收到警告并且程序将终止。我知道如何从 main 函数中通过简单的方式来完成它return 0,但是我远离 main 的多个函数调用,并且很难做到这一点,即使return 0这个函数中的 a 会保持循环。
Is there any immediate way to terminate the entire program without being in main?
有没有什么直接的方法可以在不进入主程序的情况下终止整个程序?
回答by K-ballo
The standard function exitis what you are looking for:
标准功能exit是您正在寻找的功能:
Terminates the process normally, performing the regular cleanup for terminating processes.
First, all functions registered by calls to atexit are executed in the reverse order of their registration. Then, all streams are closed and the temporary files deleted, and finally the control is returned to the host environment.
The status argument is returned to the host environment.
正常终止进程,为终止进程执行定期清理。
首先,所有通过调用 atexit 注册的函数都按照它们注册的相反顺序执行。然后,关闭所有流并删除临时文件,最后将控制权返回给宿主环境。
status 参数返回到主机环境。
It would be better though if you fixed the segfault error.
不过如果你修复了段错误会更好。
回答by Heisenbug
You need to include the standard lib and then you can call exit wherever you want:
您需要包含标准库,然后您可以在任何地方调用 exit:
#include <stdlib.h>
...
exit(status);
where status is an integer representing the exit code. For what concern status: for the convention 0 is success, other values indicates an error status.
其中 status 是一个表示退出代码的整数。对于什么关注状态:对于约定 0 表示成功,其他值表示错误状态。
回答by Basile Starynkevitch
You can also fprintf(stderr, ....)and then call abort(); this can be helpful if you want to debug later your bug.
你也可以fprintf(stderr, ....)然后调用abort();如果您想稍后调试您的错误,这可能会有所帮助。
But I believe you should recode your program so that size limitations are only given by available resources: so if you run your program on a much biggercomputer (whatever that means) it could process more than 100 inputs.
但是我相信您应该重新编码您的程序,以便大小限制仅由可用资源给出:因此,如果您在更大的计算机上运行您的程序(无论这意味着什么),它可以处理超过 100 个输入。
回答by bazza
A slightly better thing to do might be to catch the segmentation fault with a signal handler, and have the signal handler raise a SIGSTOP signal.
稍微好一点的做法可能是使用信号处理程序捕获分段错误,并让信号处理程序发出 SIGSTOP 信号。
That way your program stops, but stays in memory. You can then attach to it with a debugger and see the program in all its glory frozen in place at the point the SEGFAULT happened.
这样你的程序就会停止,但会留在内存中。然后,您可以使用调试器附加到它,并查看程序在 SEGFAULT 发生时冻结在原地的所有荣耀。
That can be useful for finding problems in long running programs that you aren't running inside a development IDE.
这对于在您未在开发 IDE 中运行的长时间运行的程序中查找问题非常有用。

