phi*_*urn 10 c exit atexit longjmp
如果我放在atexit( fn );退出堆栈上,它将在程序退出时执行:从main()或从中返回exit().
我可以从堆栈中删除它吗?
你问我为什么要这样做?
我正在尝试使用简单的try-catch机制atexit,setjmp和longjmp.如果可以的undo-atexit(fn);话,这将是完美的- 即使它只适用于最后注册的功能.
编辑:
在monoceres建议我自己的堆栈...
堆栈现在仅适用于一个异常捕获器.
void (*_catchFn[10])() = {0,0,0,0,0,0,0,0,0,0};
void _catch(){
if ( _catchFn[0] != 0 ){
(_catchFn[0])();
}
}
void _addCatch( void (*fn)() ){
_catchFn[0]=fn;
}
void _remCatch( void (*fn)() ){
_catchFn[0]=0;
}
void test(){
jmp_buf env;
void catch(){ // we get here after an exit with a registered catch
longjmp(env,1); // return to the line marked except...
// that first will get the value 1
}
int first = setjmp( env); // ** return here **
fprintf( stderr , "test: After setjmp. first=%d\n" , first );
if( first == 0 ){ // try this code
_addCatch(catch); // register the catch function to 'catch' the exit
fprintf( stderr , "test: Before CHECK\n" );
// CHECK something and something bad happens and it exits
exit(1); // like this
fprintf( stderr , "test: After CHECK - THIS SHOULD NEVER BE SEEN AFTER AN EXCEPTION.\n" );
}else{
fprintf( stderr , "test: After longjmp return. first=%d\n" , first );
}
_remCatch( catch);
fprintf( stderr , "test: IT WORKED!\n");
exit(1); // exit again to see if we are safe
}
int main(){
atexit( _catch ); // register my global exception stack
test();
}
Run Code Online (Sandbox Code Playgroud)
不,你不能这样做,但是你可以使用全局标志,所以如果设置了标志,你的退出处理程序将不会做任何事情.
或者你可以调用_Exit()(C99) - 它将执行正常的退出程序(关闭所有打开的描述符,发送所有需要的信号和父/子)但不会调用退出处理程序.