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)Objects with static storage duration are destroyed and functions registered by calling
std::atexitare called:
a) Non-local objects with static storage duration are destroyed in the reverse order of the completion of their constructor.
b)Functions registered with
std::atexitare called in the reverse order of their registration, except that a function is called after any previously registered functions that had already been called at the time it was registered.
c)For each function
fregistered with
std::atexitand each non-local object
objof static storage duration,
For each local object
objwith static storage duration,
objis destroyed as if a function calling the destructor of
objwere registered with
std::atexitat the completion of the constructor of
obj.
(until C++11) 1)The destructors of objects with thread local
storage durationthat are associated with the current thread, the destructors of objects with static storage duration, and the functions registered with
std::atexitare executed concurrently, while maintaining the following guarantees:
a)The last destructor for thread-local objects is
sequenced-beforethe first destructor for a static object.
b)If the completion of the constructor or
dynamic initializationfor 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.
c)If the completion of the initialization of a static object A was sequenced-before the call to
std::atexitfor 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::atexitfor some function F was sequenced-before 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::atexitfor some function F1 was sequenced-before the call to
std::atexitfor some function F2, then the call to F2 during termination is sequenced-before the call to F1.
(since C++11)atexit
or any destructor of static/thread-local object throws an exception, std::terminate is called.2) All C streams are flushed and closed.
4)Control is returned to the host environment. If
exit_code
is
0or
EXIT_SUCCESS, an implementation-defined status indicating successful termination is returned. If
exit_code
is
EXIT_FAILURE, an implementation-defined status indicating unsuccessful termination is returned. In other cases implementation-defined status value is returned.
Stack is not unwound: destructors of variables with automatic storage duration are not called.
[edit] Relationship with the main functionReturning 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 return was used) as exit_code
.
(none)
[edit] Example#include <cstdlib> #include <iostream> struct Static { ~Static() { std::cout << "Static destructor\n"; } }; struct Local { ~Local() { std::cout << "Local destructor\n"; } }; Static static_variable; // Destructor of this object *will* be called void atexit_handler() { std::cout << "atexit handler\n"; } int main() { Local local_variable; // Destructor 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 destructor[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior LWG 3 C++98 during cleanup, the behavior was unclear when (1) a function isRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4