Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/program/abort"

From cppreference.com
< cpp‎ | utility‎ | program
m (Shorten template names. Use {{lc}} where appropriate.)
m (fmt; update links as suggested)
 
(31 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{cpp/title| abort}}
+
{{cpp/title|abort}}
 
{{cpp/utility/program/navbar}}
 
{{cpp/utility/program/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | cstdlib}}
+
{{dcl header|cstdlib}}
{{dcl | notes={{mark since c++11}} |
+
{{dcl rev multi|until1=c++11|dcl1=
[[noreturn]] void abort();
+
void abort();
}}
+
|dcl2=
{{dcl | notes={{mark until c++11}} |
+
[[noreturn]] void abort() noexcept;
             void abort();
+
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Causes abnormal program termination unless {{rlpt|SIG_types|SIGABRT}} is being caught by a signal handler passed to signal and the handler does not return.
+
Causes abnormal program termination unless {{lc|SIGABRT}} is being caught by a signal handler passed to {{lc|std::signal}} and the handler does not return.
  
Destructors of variables with automatic, thread local and static [[cpp/language/storage duration | storage durations]] are not called. Functions, passed to {{rlpf|atexit}} are also not called. Whether open resources such as files are closed is implementation defined. Implementation defined status is returned to the host environment that indicates unsuccessful execution.  
+
Destructors of variables with automatic{{rev inl|since=c++11|, thread local}} and static {{lt|cpp/language/storage duration}}s are not called. Functions registered with {{lc|std::atexit()}} {{rev inl|since=c++11|and {{lc|std::at_quick_exit}}}} 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.  
  
 
===Parameters===
 
===Parameters===
Line 19: Line 18:
  
 
===Return value===
 
===Return value===
(none)
+
None because it does not return.
  
 
===Exceptions===
 
===Exceptions===
{{noexcept}}
+
Throws nothing.
 +
 
 +
===Notes===
 +
POSIX specifies that the [https://pubs.opengroup.org/onlinepubs/9699919799/functions/abort.html {{tt|abort()}}] function overrides blocking or ignoring the {{tt|SIGABRT}} signal.
 +
 
 +
Some compiler intrinsics, e.g. [https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html {{tt|__builtin_trap}}] (gcc, clang, and icc) or [https://learn.microsoft.com/en-us/cpp/intrinsics/fastfail {{tt|__fastfail}}]/[https://learn.microsoft.com/en-us/cpp/intrinsics/debugbreak {{tt|__debugbreak}}] (msvc), can be used to terminate the program as fast as possible.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <csignal>
  | output=
+
#include <cstdlib>
 +
#include <iostream>
 +
 
 +
class Tester
 +
{
 +
public:
 +
    Tester() { std::cout << "Tester ctor\n"; }
 +
    ~Tester() { std::cout << "Tester dtor\n"; }
 +
};
 +
 
 +
Tester static_tester; // Destructor not called
 +
 
 +
void signal_handler(int signal)
 +
{
 +
    if (signal == SIGABRT)
 +
        std::cerr << "SIGABRT received\n";
 +
    else
 +
        std::cerr << "Unexpected signal " << signal << " received\n";
 +
    std::_Exit(EXIT_FAILURE);
 +
}
 +
 
 +
int main()
 +
{
 +
    Tester automatic_tester; // Destructor not called
 +
 
 +
    // Setup handler
 +
    auto previous_handler = std::signal(SIGABRT, signal_handler);
 +
    if (previous_handler == SIG_ERR)
 +
    {
 +
        std::cerr << "Setup failed\n";
 +
        return EXIT_FAILURE;
 +
    }
 +
 
 +
    std::abort(); // Raise SIGABRT
 +
    std::cout << "This code is unreachable\n";
 +
}
 +
|output=
 +
Tester ctor
 +
Tester ctor
 +
SIGABRT received
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/utility/program/dcl list exit}}
+
{{dsc inc|cpp/utility/program/dsc exit}}
{{dsc inc | cpp/utility/program/dcl list atexit}}
+
{{dsc inc|cpp/utility/program/dsc atexit}}
{{dsc inc | cpp/utility/program/dcl list quick_exit}}
+
{{dsc inc|cpp/utility/program/dsc quick_exit}}
{{dsc see c | c/program/abort}}
+
{{dsc inc|cpp/utility/program/dsc at_quick_exit}}
 +
{{dsc inc|cpp/utility/program/dsc signal}}
 +
{{dsc inc|cpp/error/dsc terminate}}
 +
{{dsc see c|c/program/abort}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/utility/program/abort]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|tr|zh}}
[[es:cpp/utility/program/abort]]
+
[[fr:cpp/utility/program/abort]]
+
[[it:cpp/utility/program/abort]]
+
[[ja:cpp/utility/program/abort]]
+
[[pt:cpp/utility/program/abort]]
+
[[ru:cpp/utility/program/abort]]
+
[[tr:cpp/utility/program/abort]]
+
[[zh:cpp/utility/program/abort]]
+

Latest revision as of 01:30, 30 December 2023

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <cstdlib>
void abort();
(until C++11)
[[noreturn]] void abort() noexcept;
(since C++11)

Causes abnormal program termination unless SIGABRT is being caught by a signal handler passed to std::signal and the handler does not return.

Destructors of variables with automatic, thread local(since C++11) and static storage durations are not called. Functions registered with std::atexit() and std::at_quick_exit(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.

Contents

[edit] Parameters

(none)

[edit] Return value

None because it does not return.

[edit] Exceptions

Throws nothing.

[edit] Notes

POSIX specifies that the abort() function overrides blocking or ignoring the SIGABRT signal.

Some compiler intrinsics, e.g. __builtin_trap (gcc, clang, and icc) or __fastfail/__debugbreak (msvc), can be used to terminate the program as fast as possible.

[edit] Example

#include <csignal>
#include <cstdlib>
#include <iostream>
 
class Tester
{
public:
    Tester()  { std::cout << "Tester ctor\n"; }
    ~Tester() { std::cout << "Tester dtor\n"; }
};
 
Tester static_tester; // Destructor not called
 
void signal_handler(int signal) 
{
    if (signal == SIGABRT)
        std::cerr << "SIGABRT received\n";
    else
        std::cerr << "Unexpected signal " << signal << " received\n";
    std::_Exit(EXIT_FAILURE);
}
 
int main()
{
    Tester automatic_tester; // Destructor not called
 
    // Setup handler
    auto previous_handler = std::signal(SIGABRT, signal_handler);
    if (previous_handler == SIG_ERR)
    {
        std::cerr << "Setup failed\n";
        return EXIT_FAILURE;
    }
 
    std::abort(); // Raise SIGABRT
    std::cout << "This code is unreachable\n";
}

Output:

Tester ctor
Tester ctor
SIGABRT received

[edit] See also

causes normal program termination with cleaning up
(function) [edit]
registers a function to be called on std::exit() invocation
(function) [edit]
causes quick program termination without completely cleaning up
(function) [edit]
registers a function to be called on std::quick_exit invocation
(function) [edit]
sets a signal handler for particular signal
(function) [edit]
function called when exception handling fails
(function) [edit]
C documentation for abort