C++中abort和exit的区别

abort会发送SIGABORT信号

调用exit后,程序会调用静态对象全局对象的析构函数,但abor什么析构函数都不会调用。

程序完全退出时,系统会释放所有未释放的内存和和其他资源

abort sends a SIGABRT signal, exit just closes the application performing normal cleanup.

You can handle an abort signal however you want, but the default behavior is to close the application as well with an error code.

abort will not perform object destruction of your static and global members, but exit will.

Of course though when the application is completely closed the operating system will free up any unfreed memory and other resources.

In both abort and exit program termination (assuming you didn't override the default behavior), the return code will be returned to the parent process that started your application.

See the following example:

SomeClassType someobject;

void myProgramIsTerminating1(void)
{
  cout<<"exit function 1"<<endl;
}

void myProgramIsTerminating2(void)
{
  cout<<"exit function 2"<<endl;
}

int main(int argc, char**argv)
{
  atexit (myProgramIsTerminating1);
  atexit (myProgramIsTerminating2);
  //abort();
  return 0;
}

Comments:

  • If abort is uncommented: nothing is printed and the destructor of someobject will not be called.

  • If abort is commented like above: someobject destructor will be called you will get the following output:

exit function 2
exit function 1

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值