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

2019-07-18 11:06发布

问题:

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)



标签: c++ abort