我们是否应该使用C出口()? [英] Should we use exit() in C?

查看:114
本文介绍了我们是否应该使用C出口()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于使用退出在C ++中。答案讨论,这不是好主意,因为RAII为主,例如,如果退出在code的地方叫,对象的析构函数不会被调用,因此,例如,如果一个析构函数是为了将数据写入到文件,这不会发生,因为析构函数不叫。

我很感兴趣,这是怎么情况,C中类似的问题也适用于C 2我想既然用C我们不使用构造函数/析构函数,情况可能会在不同C.所以它是确定使用在C出口

我见过的功能,如下面,我觉得这罚款在某些情况下使用,但很感兴趣,如果我们用C类似的问题,使用退出,描述以上C ++? (这将使使用功能,如下面不是一个好主意)。

 无效模具(为const char *消息)
{
    如果(错误){
        PERROR(消息);
    }其他{
        的printf(错误:%s \\ n,邮件);
    }    出口(1);
}


解决方案

而不是中止()退出()的C函数被认为是一个优雅的退出。

从C11(N1570)7.22.4.4/p2的 exit函数的(重点煤矿):


  

退出函数导致发生的常规程序终止。


该标准还表示,在该7.22.4.4/p4:


  

接下来,是未写入缓冲数据的所有打开的流被刷新后,所有
  开放式流是的关闭,然后通过 TMPFILE 函数创建的所有文件
  被除去。


这也是值得看的7.21.3 / P5的文件的:


  

如果在函数返回到原来的调用者,或者如果退出
  函数被调用,所有打开的文件被关闭(因此所有的输出
  流是计划终止前刷新)。到其他路径
  程序终止,如调用中止功能,无须
  正确地关闭所有文件。


不过,正如下面你意见所不能假设它会覆盖所有其他的资源的,所以你可能需要求助于的atexit()并分别定义其释放回调。事实上,它到底是什么的atexit()打算做的,因为它7.22.4.2/p2说的里的AtExit功能的:


  

atexit对功能寄存器指向的函数 FUNC ,是
  所谓不正常程序终止参数。


值得注意的是,C标准不说$ P​​ $ pcisely应该发生什么对象的分配存储时间的(即的malloc()) ,因此需要你知道它是如何在具体的实施完成。对于现代,面向主机的操作系统很可能系统会照顾它,但你仍然会想自己以沉默内存调试等的 Valgrind的

There is question about using exit in C++. The answer discusses that it is not good idea mainly because of RAII, e.g., if exit is called somewhere in code, destructors of objects will not be called, hence, if for example a destructor was meant to write data to file, this will not happen, because the destructor was not called.

I was interested how is this situation in C. Are similar issues applicable also in C? I thought since in C we don't use constructors/destructors, situation might be different in C. So is it ok to use exit in C?

I have seen functions such as below, which I find fine to use in some cases, but was interested if we have similar issues in C with using exit, as described above with C++? (which would make using functions such as below not a good idea.).

void die(const char *message)
{
    if(errno) {
        perror(message);
    } else {
        printf("ERROR: %s\n", message);
    }

    exit(1);
}

解决方案

Rather than abort(), the exit() function in C is considered to be a "graceful" exit.

From C11 (N1570) 7.22.4.4/p2 The exit function (emphasis mine):

The exit function causes normal program termination to occur.

The Standard also says in 7.22.4.4/p4 that:

Next, all open streams with unwritten buffered data are flushed, all open streams are closed, and all files created by the tmpfile function are removed.

It is also worth looking at 7.21.3/p5 Files:

If the main function returns to its original caller, or if the exit function is called, all open files are closed (hence all output streams are flushed) before program termination. Other paths to program termination, such as calling the abort function, need not close all files properly.

However, as mentioned in comments below you can't assume that it will cover every other resource, so you may need to resort to atexit() and define callbacks for their release individually. In fact it is exactly what atexit() is intended to do, as it says in 7.22.4.2/p2 The atexit function:

The atexit function registers the function pointed to by func, to be called without arguments at normal program termination.

Notably, the C standard does not say precisely what should happen to objects of allocated storage duration (i.e. malloc()), thus requiring you be aware of how it is done on particular implementation. For modern, host-oriented OS it is likely that the system will take care of it, but still you might want to handle this by yourself in order to silence memory debuggers such as Valgrind.

这篇关于我们是否应该使用C出口()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆