如何立即关闭C语言中的程序?

时间:2023-01-17 10:51:19

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 0 in this function will keep it looping.

我正在编写C代码,在其中分析一些数据。我将程序设置为只处理100个数据输入。当它有超过100个输入时,就会产生分割错误。我想创建一种方法,当输入数量超过100时,用户将被警告,程序将终止。我知道如何从主函数返回0,但是我有多个函数调用远离主函数,即使函数中的返回0也会保持循环。

Is there any immediate way to terminate the entire program without being in main?

有没有什么直接的方法可以在不使用主程序的情况下终止整个程序?

4 个解决方案

#1


34  

The standard function exit is what you are looking for:

标准函数出口就是你要找的:

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.

首先,对atexit调用所注册的所有函数都按照它们的注册顺序执行。然后,关闭所有流并删除临时文件,最后将控件返回到宿主环境。

The status argument is returned to the host environment.

状态参数返回给主机环境。

It would be better though if you fixed the segfault error.

如果你修正了segfault错误,那就更好了。

#2


5  

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.

其中状态是表示退出代码的整数。对于关注状态:对于约定0是成功,其他值表示错误状态。

#3


2  

You can also fprintf(stderr, ....) and then call abort(); this can be helpful if you want to debug later your bug.

您还可以进行fprintf(stderr,....),然后调用abort();如果您希望稍后调试您的bug,这将非常有用。

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 bigger computer (whatever that means) it could process more than 100 inputs.

但是我认为您应该重新编码您的程序,以便只有可用的资源才能提供大小限制:因此,如果您在一个大得多的计算机上运行您的程序(无论这意味着什么),它可以处理100多个输入。

#4


1  

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中不运行的长期运行程序中发现问题非常有用。

#1


34  

The standard function exit is what you are looking for:

标准函数出口就是你要找的:

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.

首先,对atexit调用所注册的所有函数都按照它们的注册顺序执行。然后,关闭所有流并删除临时文件,最后将控件返回到宿主环境。

The status argument is returned to the host environment.

状态参数返回给主机环境。

It would be better though if you fixed the segfault error.

如果你修正了segfault错误,那就更好了。

#2


5  

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.

其中状态是表示退出代码的整数。对于关注状态:对于约定0是成功,其他值表示错误状态。

#3


2  

You can also fprintf(stderr, ....) and then call abort(); this can be helpful if you want to debug later your bug.

您还可以进行fprintf(stderr,....),然后调用abort();如果您希望稍后调试您的bug,这将非常有用。

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 bigger computer (whatever that means) it could process more than 100 inputs.

但是我认为您应该重新编码您的程序,以便只有可用的资源才能提供大小限制:因此,如果您在一个大得多的计算机上运行您的程序(无论这意味着什么),它可以处理100多个输入。

#4


1  

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中不运行的长期运行程序中发现问题非常有用。