Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::abort

      From cppreference.com
      <cpp‎ |utility‎ |program
       
       
      Utilities library
       
       
      Defined in header<cstdlib>
      void abort();
      (until C++11)
      [[noreturn]]void abort()noexcept;
      (since C++11)

      Causes abnormal program termination unlessSIGABRT is being caught by a signal handler passed tostd::signal and the handler does not return.

      Destructors of variables with automatic, thread local(since C++11) and staticstorage durations are not called. Functions registered withstd::atexit()andstd::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 theabort() function overrides blocking or ignoring theSIGABRT 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

      Run this code
      #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";elsestd::cerr<<"Unexpected signal "<< signal<<" received\n";std::_Exit(EXIT_FAILURE);} int main(){    Tester automatic_tester;// Destructor not called // Setup handlerauto previous_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 ctorTester ctorSIGABRT received

      [edit]See also

      causes normal program termination with cleaning up
      (function)[edit]
      registers a function to be called onstd::exit() invocation
      (function)[edit]
      (C++11)
      causes quick program termination without completely cleaning up
      (function)[edit]
      registers a function to be called onstd::quick_exit invocation
      (function)[edit]
      sets a signal handler for particular signal
      (function)[edit]
      function called when exception handling fails
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/program/abort&oldid=167779"

      [8]ページ先頭

      ©2009-2025 Movatter.jp