18 Language support library [language.support]

18.5 Start and termination [support.start.term]

Table [tab:support.hdr.cstdlib] describes some of the contents of the header <cstdlib>.

Table 33 — Header <cstdlib> synopsis
TypeName(s)
Macros: EXIT_FAILURE EXIT_SUCCESS
Functions: _Exit abort atexit
at_quick_exit exit quick_exit

The contents are the same as the Standard C library header <stdlib.h>, with the following changes:

[[noreturn]] void _Exit(int status) noexcept;

The function _Exit(int status) has additional behavior in this International Standard:

  • The program is terminated without executing destructors for objects of automatic, thread, or static storage duration and without calling functions passed to atexit() ([basic.start.term]).

[[noreturn]] void abort(void) noexcept;

The function abort() has additional behavior in this International Standard:

  • The program is terminated without executing destructors for objects of automatic, thread, or static storage duration and without calling functions passed to atexit() ([basic.start.term]).

extern "C" int atexit(void (*f)(void)) noexcept; extern "C++" int atexit(void (*f)(void)) noexcept;

Effects: The atexit() functions register the function pointed to by f to be called without arguments at normal program termination. It is unspecified whether a call to atexit() that does not happen before ([intro.multithread]) a call to exit() will succeed. [ Note: The atexit() functions do not introduce a data race ([res.on.data.races]).  — end note ]

Implementation limits: The implementation shall support the registration of at least 32 functions.

Returns: The atexit() function returns zero if the registration succeeds, non-zero if it fails.

[[noreturn]] void exit(int status)

The function exit() has additional behavior in this International Standard:

  • First, objects with thread storage duration and associated with the current thread are destroyed. Next, objects with static storage duration are destroyed and functions registered by calling atexit are called.224 See [basic.start.term] for the order of destructions and calls. (Automatic objects are not destroyed as a result of calling exit().)225

    If control leaves a registered function called by exit because the function does not provide a handler for a thrown exception, std::terminate() shall be called ([except.terminate]).

  • Next, all open C streams (as mediated by the function signatures declared in <cstdio>) with unwritten buffered data are flushed, all open C streams are closed, and all files created by calling tmpfile() are removed.

  • Finally, control is returned to the host environment. If status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.226

extern "C" int at_quick_exit(void (*f)(void)) noexcept; extern "C++" int at_quick_exit(void (*f)(void)) noexcept;

Effects: The at_quick_exit() functions register the function pointed to by f to be called without arguments when quick_exit is called. It is unspecified whether a call to at_quick_exit() that does not happen before ([intro.multithread]) all calls to quick_exit will succeed. [ Note: The at_quick_exit() functions do not introduce a data race ([res.on.data.races]).  — end note ] [ Note: The order of registration may be indeterminate if at_quick_exit was called from more than one thread.  — end note ] [ Note: The at_quick_exit registrations are distinct from the atexit registrations, and applications may need to call both registration functions with the same argument.  — end note ]

Implementation limits: The implementation shall support the registration of at least 32 functions.

Returns: Zero if the registration succeeds, non-zero if it fails.

[[noreturn]] void quick_exit(int status) noexcept;

Effects: Functions registered by calls to at_quick_exit are called in the reverse order of their registration, except that a function shall be called after any previously registered functions that had already been called at the time it was registered. Objects shall not be destroyed as a result of calling quick_exit. If control leaves a registered function called by quick_exit because the function does not provide a handler for a thrown exception, std::terminate() shall be called.Note: at_quick_exit may call a registered function from a different thread than the one that registered it, so registered functions should not rely on the identity of objects with thread storage duration.  — end note ] After calling registered functions, quick_exit shall call _Exit(status). [ Note: The standard file buffers are not flushed. See: ISO C 7.20.4.4.  — end note ]

See also: [basic.start], [basic.start.term], ISO C 7.10.4.

A function is called for every time it is registered.

Objects with automatic storage duration are all destroyed in a program whose function main() contains no automatic objects and executes the call to exit(). Control can be transferred directly to such a main() by throwing an exception that is caught in main().

The macros EXIT_FAILURE and EXIT_SUCCESS are defined in <cstdlib>.