Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::exit

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

      Causes normal program termination to occur.

      Several cleanup steps are performed:

      1) Objects with static storage duration are destroyed and functions registered by callingstd::atexit are called:
      a) Non-local objects with static storage duration are destroyed in the reverse order of the completion of their constructor.
      b) Functions registered withstd::atexit are called in the reverse order of their registration, except that a function is called after any previously registered functions that had already been called at the time it was registered.
      c) For each functionf registered withstd::atexit and each non-local objectobj of static storage duration,
      • iff is registered before the initialization ofobj,f will only be called after the destruction ofobj;
      • iff is registered after the initialization ofobj,f will only be called before the destruction ofobj.
      d) For each local objectobj with static storage duration,obj is destroyed as if a function calling the destructor ofobj were registered withstd::atexit at the completion of the constructor ofobj.
      (until C++11)
      1) The destructors of objects with thread localstorage duration that are associated with the current thread, the destructors of objects with static storage duration, and the functions registered withstd::atexit are executed concurrently, while maintaining the following guarantees:
      a) The last destructor for thread-local objects issequenced-before the first destructor for a static object.
      b) If the completion of the constructor ordynamic initialization for thread-local or static object A was sequenced-before thread-local or static object B, the completion of the destruction of B is sequenced-before the start of the destruction of A.
      c) If the completion of the initialization of a static object A was sequenced-before the call tostd::atexit for some function F, the call to F during termination is sequenced-before the start of the destruction of A.
      d) If the call tostd::atexit for some function F was sequenced-before the completion of initialization of a static object A, the start of the destruction of A is sequenced-before the call to F during termination.
      e) If a call tostd::atexit for some function F1 was sequenced-before the call tostd::atexit for some function F2, then the call to F2 during termination is sequenced-before the call to F1.
      (since C++11)
      • In the above,
      • If any function registered withatexit or any destructor of static/thread-local object throws an exception,std::terminate is called.
      • If the compiler opted to lift dynamic initialization of an object to the static initialization phase ofnon-local initialization, the sequencing of destruction honors its would-be dynamic initialization.
      • If a function-local (block-scope) static object was destroyed and then that function is called from the destructor of another static object and the control flow passes through the definition of that object (or if it is used indirectly, via pointer or reference), the behavior is undefined.
      • If a function-local (block-scope) static object was initialized during construction of a subobject of a class or array, it is only destroyed after all subobjects of that class or all elements of that array were destroyed.
      2) All C streams are flushed and closed.
      3) Files created bystd::tmpfile are removed.
      4) Control is returned to the host environment. Ifexit_code is0 orEXIT_SUCCESS, an implementation-defined status indicating successful termination is returned. Ifexit_code isEXIT_FAILURE, an implementation-defined status indicating unsuccessful termination is returned. In other cases implementation-defined status value is returned.

      Stack is not unwound: destructors of variables with automaticstorage duration are not called.

      Contents

      [edit]Relationship with the main function

      Returning from themain function, either by areturn statement or by reaching the end of the function performs the normal function termination (calls the destructors of the variables with automaticstorage durations) and then executesstd::exit, passing the argument of the return statement (or0 if implicit return was used) asexit_code.

      [edit]Parameters

      exit_code - exit status of the program

      [edit]Return value

      (none)

      [edit]Example

      Run this code
      #include <cstdlib>#include <iostream> struct Static{    ~Static(){std::cout<<"Static destructor\n";}}; struct Local{    ~Local(){std::cout<<"Local destructor\n";}}; Static static_variable;// Destructor of this object *will* be called void atexit_handler(){std::cout<<"atexit handler\n";} int main(){    Local local_variable;// Destructor of this object will *not* be calledconstint result=std::atexit(atexit_handler);// Handler will be called if(result!=0){std::cerr<<"atexit registration failed\n";returnEXIT_FAILURE;} std::cout<<"test\n";    std::exit(EXIT_FAILURE); std::cout<<"this line will *not* be executed\n";}

      Output:

      testatexit handlerStatic destructor

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3C++98during cleanup, the behavior was unclear when (1) a function is
      registered withstd::atexit or (2) a static local object is initialized
      made clear

      [edit]See also

      causes abnormal program termination (without 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]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/program/exit&oldid=161404"

      [8]ページ先頭

      ©2009-2025 Movatter.jp