You can call abort(), exit(), quick_exit and _exit to end you problem
anytime. they are both declared in <cstdlib> head file.
exit( status ) terminates the process normally. a status value of 0 or
EXIT_SUCCESS indicates success, and any other value or the constant
EXIT_FAILURE is used to indicate an error. exit() performs following
operations. 1) Flushes unwritten buffered data. 2) Closes all open files.
3) Removes temporary files.
atexit() Registers the function pointed to by func to be called on normal
program termination (via std::exit() or returning from the main function)
C++11 introduce quick_exit. It was added to specifically deal with
the difficulty of ending a program cleanly when you use threads.
std::quick_exit() is similar to _exit() but with still the option to execute
some code, whatever was registered with at\_quick\_exit.
_exit() is called without performing any of the regular cleanup tasks for
terminating processes
abort() is called without destroying any object and without calling any of
the functions passed to atexit or at_quick_exit. But It will dump core, if
the user has core dumps enabled. Using abort to debug by analysing a core
dump.
When you use gdb, abort can list stack frame information for you.
It’s very helpful for you debug information. Exit just end the application.
When you use gdb, it show nothing.
assert just call abort. You can use assert in this way.
assert(!"You␣should␣not␣reach␣here");
std::terminate is what is automatically called in a C++ program
when there is an unhandled C++ exception. This is essentiallythe C++ equivalent to abort. This calls a handler that is set
by the std::set_terminate function, which by default simply calls
abort.
Don’t use exit in main, It will not destroy local object in main function.
Catch the exceptions you can’t handle in main() and simply return from
there. This means that you are guaranteed that stack unwinding happens
correctly and all destructors are called.
according to previous main, if you want to end applicaiton in the
other fun, you need to throw a exception, then leave it un-handle or
rethrow it until it reach main, in this way, stack unwinding will
make sure all the destrcutor will be called. Don’t use exit, it’sC-style function and will not perform any stack unwinding