【C++】cstdlib中exit/abort/atexit

http://www.cplusplus.com/reference/clibrary/cstdlib/abort/

http://www.cplusplus.com/reference/clibrary/cstdlib/exit/

http://www.cplusplus.com/reference/clibrary/cstdlib/atexit/

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上注册的函数先被执行(如果有多个则按与注册顺序相反的顺序依次执行),然后释放缓冲区,关闭所有I/O通道,终止所有程序,最终将控制权交回主环境。


abort

Aborts the process with an abnormal program termination.
The function generates the SIGABRT signal, which by default causes the program to terminate returning an unsuccessful termination error code to the host environment.

The program is terminated without executing destructors for objects of automatic or static storage duration, and without calling any atexit function.
异常终止所有程序。

此函数会生成SIGABRT信号,默认导致程序终止并返回失败终止错误代码给主环境。

此函数终止时不会执行动态或静态存储区对象的析构,不会调用atexit函数。

atexit

int atexit ( void ( * function ) (void) );

Set function to be executed on exit

The function pointed by the  function pointer argument is called when the program terminates normally.

If more than one  atexit function has been specified by different calls to this function, they are all executed in reverse order as a stack, i.e. the last function specified is the first to be executed at exit.

One single function can be registered to be executed at exit more than once.

一个函数可以在退出时多次注册执行。

/* atexit example */
#include <stdio.h>
#include <stdlib.h>

void fnExit1 (void)
{
  puts ("Exit function 1.");
}

void fnExit2 (void)
{
  puts ("Exit function 2.");
}

int main ()
{
  atexit (fnExit1);
  atexit (fnExit2);
  puts ("Main function.");
  return 0;
}
结果:
Main function.
Exit function 2.
Exit function 1.



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值