From cppreference.com
Defined in header
voidabort();(until C++11)[[noreturn]]voidabort()noexcept;(since C++11)Causes abnormal program termination unless
is being caught by a signal handler passed to
. The handler does not return.
Destructors of variables with automatic, thread local(since C++11) and static
are not called. Functions registered with
and
(since C++11) are also not called. Whether open resources such as files are closed is implementation defined. An implementation defined status is returned to the host environment that indicates unsuccessful execution.
Return value
None because it does not return.
Exceptions
Throws nothing.
Notes
POSIX specifies that the
function overrides blocking or ignoring the SIGABRT signal.
Some compiler intrinsics, e.g.
(gcc, clang, and icc) or
/
(msvc), can be used to terminate the program as fast as possible.
Example
Run this code
#include<csignal>#include<cstdlib>#include<iostream>classTester{public:Tester(){std::cout<<"Tester ctor\n";}~Tester(){std::cout<<"Tester dtor\n";}};Testerstatic_tester;// Destructor not calledvoidsignal_handler(intsignal){if(signal==SIGABRT)std::cerr<<"SIGABRT received\n";elsestd::cerr<<"Unexpected signal "<<signal<<" received\n";std::_Exit(EXIT_FAILURE);}intmain(){Testerautomatic_tester;// Destructor not called// Setup handlerautoprevious_handler=std::signal(SIGABRT,signal_handler);if(previous_handler==SIG_ERR){std::cerr<<"Setup failed\n";returnEXIT_FAILURE;}std::abort();// Raise SIGABRTstd::cout<<"This code is unreachable\n";}Output:
Tester ctor Tester ctor SIGABRT received See also
causes normal program termination with cleaning up
(function)
registers a function to be called on
invocation
(function)
(C++11)
causes quick program termination without completely cleaning up
(function)
(C++11)
registers a function to be called on
invocation
(function)
sets a signal handler for particular signal
(function)
function called when exception handling fails
(function)
for abort