【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.



你可能感兴趣的:(C++,function,存储,returning)