我可以撤销还是删除atexit命令?

如果我放在atexit(fn);在退出堆栈中,当程序退出时,它将被执行:从main()或exit()返回.

我可以从堆栈中删除它吗?

为什么要这样做,你问?

我正在使用一个简单的try-catch机制,使用atexit,setjmp和longjmp.如果我可以撤销atexit(fn),那将是完美的; – 即使它只适用于最后一个注册功能.

编辑:

以下单词建议使我自己的堆栈…

该堆栈只适用于一个异常捕获器.

void (*_catchFn[10])()  = {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();
}

解决方法

为什么不从单个atexit()函数构建自己的堆栈?这样你可以操纵堆栈你想要的.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...