std::atexit(3)
π std::atexit π
okay bestie, welcome to the C++ Standard Libary manual page for std::atexit (page std::atexit(3)) β yes, the βLibaryβ spelling is right there in the header and we are not fixing it, we are respecting it π
. This is the function that registers a function to run on normal program termination, and the vibe is very βcleanup crew but make it C++β β¨
πΈ NAME πΈ
std::atexit β std::atexit.
Thatβs it. Thatβs the name section. Minimal, iconic, periodt.
β¨ Synopsis β¨
Header tea first:
Defined in header <cstdlib>
int atexit( /* c-atexit-handler */* func ); (until C++11)
int atexit( /* atexit-handler */* func );
int atexit( /* c-atexit-handler */* func ) noexcept; (1) (since C++11)
int atexit( /* atexit-handler */* func ) noexcept;
extern "C" using /* c-atexit-handler */ = void(); (2)
(exposition only*)
extern "C++" using /* atexit-handler */ = void();
Okay so what does it do? It registers the function pointed to by func to be called on normal program termination β like via std::exit() or returning from the main function π«Ά.
Hereβs the ordering tea:
- The functions will be called during the destruction of the static objects, in reverse order: if A was registered before B, then the call to B is made before the call to A. Same applies to the ordering between static object constructors and the calls to
atexit: seestd::exit. (until C++11) - (since C++11) The functions may be called concurrently with the destruction of the objects with static storage duration and with each other, maintaining the guarantee that if registration of A was sequenced-before the registration of B, then the call to B is sequenced-before the call to A, same applies to the sequencing between static object constructors and calls to
atexit: seestd::exit.
Also: the same function may be registered more than once. No cap, you can double-book that handler π.
If a function exits via an exception, std::terminate is called. So bestie, do not let your exit handler yeet an exception out into the void π.
atexit is thread-safe: calling the function from several threads does not induce a data race. Slay.
The implementation is guaranteed to support the registration of at least 32 functions. The exact limit is implementation-defined. So the minimum is 32, and your implementationβs vibe check decides the rest π
.
π Parameters π
funcβ pointer to a function to be called on normal program termination.
π Return value π
0 if the registration succeeds, nonzero value otherwise. Zero is the success slay; nonzero is the βregistration said noβ energy.
π Notes π
The two overloads are distinct because the types of the parameter func are distinct β language linkage is part of its type. Yes, this is one of those C++ details that is lowkey doing the most, and it is valid, fr fr.
π¦ Example π¦
// Run this code
#include <cstdlib>
#include <iostream>
void atexit_handler_1()
{
std::cout << "At exit #1\n";
}
void atexit_handler_2()
{
std::cout << "At exit #2\n";
}
int main()
{
const int result_1 = std::atexit(atexit_handler_1);
const int result_2 = std::atexit(atexit_handler_2);
if (result_1 || result_2)
{
std::cerr << "Registration failed!\n";
return EXIT_FAILURE;
}
std::cout << "Returning from main...\n";
return EXIT_SUCCESS;
}
Output:
Returning from main...
At exit #2
At exit #1
See that reverse order? Registration order was handler 1 then handler 2, and the calls came back handler 2 then handler 1. The reverse-order rule ate and left no crumbs π .
π₯ See also π₯
abortβ causes abnormal program termination (without cleaning up) (function)exitβ causes normal program termination with cleaning up (function)quick_exitβ causes quick program termination without completely cleaning up (C++11) (function)at_quick_exitβ registers a function to be called onstd::quick_exitinvocation (C++11) (function)- C documentation for
atexit
π source/version: http://cppreference.com, date: 2024.06.10, page: std::atexit(3) β bye bestie, go register those exit handlers and exit cleanly ππ«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms