简体   繁体   中英

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

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

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

Here is the man page to read up about it.

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);
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM