• exit 函数 与上0377 十六进制377,按位与 保留低八位

    • 重要功能。所有的函数会被调用已注册的逆序。
  • atexit 钩子函数 ,类似c++的析构函数。销毁的时候。

#include<stdio.h>
#include <stdlib.h>
 
static void f1(void)
{
	puts("f1() is working!");
}
static void f2(void)
{
	puts("f2() is working!");
}
static void f3(void)
{
	puts("f3() is working!");
}
int main()
{
	puts("Begin!");
	//将三个函数挂在钩子上,在执行exit前调用
	atexit(f1);
	atexit(f2);
	atexit(f3);
	puts("End!");
	exit(0);
}
 
  • 用途:
fd1=open()
if(fd1<0)
{
	perror();
	exit(1);
}

atexit(); ---->close(fd1);

fd2=open();
if(fd2<0)
{
	close(fd1);
	perror();
	exit(1);
}
......
fd100=open();
if(fd100<0){
//要关的多了
}

  • 库函数 系统调用。 依赖型 exit依赖于_exit.调用_exit不执行钩子函数和io清理
  • f越界
  • 使用exit故障扩大
func()
{
	return 0/1/2;
}
int main()
{
 
	int f ;
	f = func();
	。。。???
	switch(f)
	{
		case 0:
		case 1:
		case 2:
		default:
		//出现了第四种情况
			_exit(1);        //abort();信号
	
	}
	
}