FreeBSD Manual Pages
std::exit(3) C++ Standard Libary std::exit(3) NAME std::exit - std::exit Synopsis Defined in header <cstdlib> void exit( int exit_code ); (until C++11) [[noreturn]] void exit( int exit_code ); (since C++11) Causes normal program termination to occur. Several cleanup steps are performed: 1) destructors of objects with static storage duration are called in reverse order of completion of their constructors or the completion of their dynamic initialization, and the functions passed to std::atexit are called in reverse order they are registered (last one first). a) any static objects whose initialization was completed before the call to std::atexit for some function F will be destroyed after the (until C++11) call to F during program termination. b) any static objects whose construction began after the call to std::atexit for some function F will be destroyed before the call to F during program termination (this includes the case where std::atexit was called from the constructor of the static object) 1) The destructors of objects with thread local storage duration that are associated with the current thread, the destructors of objects with static storage duration, and the functions registered with std::atexit are executed concurrently, while maintaining the follow- ing guarantees: a) The last destructor for thread-local objects is sequenced-before the first destructor for a static object b) If the completion of the constructor or dynamic initialization for thread-local or static object A was sequenced-before thread-local or static object B, the completion of the destruction of B is sequenced-before the start of the destruction of A (since C++11) c) If the completion of the initialization of a static object A was sequenced-before the call to std::atexit for some function F, the call to F during termination is sequenced-before the start of the destruction of A d) If the call to std::atexit for some function F was sequenced-be- fore the completion of initialization of a static object A, the start of the destruction of A is sequenced-before the call to F during termination. e) If a call to std::atexit for some function F1 was sequenced-be- fore the call to std::atexit for some function F2, then the call to F2 during termination is sequenced-before the call to F1 * In the above, * if any function registered with atexit or any destructor of static/thread-local object throws an exception, std::terminate is called * if the compiler opted to lift dynamic ini- tialization of an object to the static initialization phase of non-local initialization, the sequencing of destruc- tion honors its would-be dynamic initialization. * If a function-local (block-scope) static object was destroyed and then that function is called from the destructor of another static object and the control flow passes through the definition of that ob- ject (or if it is used indirectly, via pointer or refer- ence), the behavior is undefined. * if a function-local (block-scope) static object was initialized during construction of a sub- object of a class or array, it is only destroyed after all subobjects of that class or all elements of that array were destroyed. 2) all C streams are flushed and closed 3) files created by std::tmpfile are removed 4) control is returned to the host environment. If exit_code is 0 or EXIT_SUCCESS, an implementation-defined status indicating successful termination is returned. If exit_code is EXIT_FAILURE, an implementation-defined status indicat- ing unsuccessful termination is returned. In other cases implementation-defined sta- tus value is returned. Stack is not unwound: destructors of variables with automatic stor- age duration are not called. Relationship with the main function Returning from the main function, either by a return statement or by reaching the end of the function performs the normal function termination (calls the destructors of the variables with automatic storage durations) and then executes std::exit, passing the argument of the return statement (or 0 if implicit re- turn was used) as exit_code. Parameters exit_code - exit status of the program Return value (none) Example // Run this code #include <iostream> #include <cstdlib> struct Static { ~Static() { std::cout << "Static dtor\n"; } }; struct Local { ~Local() { std::cout << "Local dtor\n"; } }; Static static_variable; // dtor of this object *will* be called void atexit_handler() { std::cout << "atexit handler\n"; } int main() { Local local_variable; // dtor of this object will *not* be called const int result = std::atexit(atexit_handler); // handler will be called if (result != 0) { std::cerr << "atexit registration failed\n"; return EXIT_FAILURE; } std::cout << "test\n"; std::exit(EXIT_FAILURE); std::cout << "this line will *not* be executed\n"; } Output: test atexit handler Static dtor See also abort causes abnormal program termination (without cleaning up) (function) atexit registers a function to be called on std::exit() invo- cation (function) quick_exit causes quick program termination without completely cleaning up (C++11) (function) at_quick_exit registers a function to be called on std::quick_exit invocation (C++11) (function) http://cppreference.com 2022.07.31 std::exit(3)
NAME | Synopsis | Relationship with the main function | Parameters | Return value | Example | Output: | See also
Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::exit&sektion=3&manpath=FreeBSD+Ports+15.0>
