how to abort a c++ program and exit with status 0?

回眸只為那壹抹淺笑 提交于 2019-12-24 00:53:03

问题


I want to kill my c++ program and terminate immediately without activating destructors of any kind, especially static and global variables, but I want to exit with status 0 - abort() will not work for me.

Does anyone have a solution? Thanks


回答1:


Maybe _exit(0); is what you're looking for?

Here is the man page to read up about it.




回答2:


From C++11 n3290 - § 18.5:

[[noreturn]] void _Exit(int status) noexcept;

The program is terminated without executing destructors for objects of automatic, thread, or static storage duration and without calling functions passed to atexit()

This is actually defined in C99 though so in practice works on a large number of pre-C++11 implementations.

Use:

#include <cstdlib>
#include <iostream>

struct test {
  ~test() {
    std::cout << "Goodbye world" << std::endl;
  }
};

int main() {
  test t;
  _Exit(0);
}



回答3:


How about _Exit(0) from stdlib.h. (Demo: http://ideone.com/ecCgC)



来源:https://stackoverflow.com/questions/8804643/how-to-abort-a-c-program-and-exit-with-status-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!