Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      setjmp

      From cppreference.com
      <c‎ |program
       
       
      Program support utilities
      Program termination
      Unreachable control flow
      Communicating with the environment
      Memory alignment query
      Signals
      Signal types
      Non-local jumps
      setjmp
      Types
       
      Defined in header<setjmp.h>
      #define setjmp(env) /* implementation-defined */

      Saves the current execution context into a variableenv of typejmp_buf. This variable can later be used to restore the current execution context bylongjmp function. That is, when a call tolongjmp function is made, the execution continues at the particular call site that constructed thejmp_buf variable passed tolongjmp. In that casesetjmp returns the value passed tolongjmp.

      The invocation ofsetjmp must appear only in one of the following contexts:

      1. The entire controlling expression ofif,switch,while,do-while,for.
        switch(setjmp(env)){// ...
      2. One operand of a relational or equality operator with the other operand an integer constant expression, with the resulting expression being the entire controlling expression ofif,switch,while,do-while,for.
        if(setjmp(env)>10){// ...
      3. The operand of a unary ! operator with the resulting expression being the entire controlling expression ofif,switch,while,do-while,for.
        while(!setjmp(env)){// ...
      4. The entire expression of anexpression statement (possibly cast tovoid).
        setjmp(env);

      Ifsetjmp appears in any other context, the behavior is undefined.

      Upon return to the scope ofsetjmp:

      • all accessible objects, floating-point status flags, and other components of the abstract machine have the same values as they had whenlongjmp was executed,
      • except for the non-volatile local variables in the function containing the invocation ofsetjmp, whose values are indeterminate if they have been changed since thesetjmp invocation.

      Contents

      [edit]Parameters

      env - variable to save the execution state of the program to.

      [edit]Return value

      0 if the macro was called by the original code and the execution context was saved toenv.

      Non-zero value if a non-local jump was just performed. The return value is the same as passed tolongjmp.

      [edit]Notes

      Above requirements forbid using return value ofsetjmp in data flow (e.g. to initialize or assign an object with it). The return value can only be either used in control flow or discarded.

      [edit]Example

      Run this code
      #include <stdio.h>#include <setjmp.h>#include <stdnoreturn.h> jmp_buf my_jump_buffer; noreturnvoid foo(int status){printf("foo(%d) called\n", status);longjmp(my_jump_buffer, status+1);// will return status+1 out of setjmp} int main(void){volatileint count=0;// modified local vars in setjmp scope must be volatileif(setjmp(my_jump_buffer)!=5)// compare against constant in an if        foo(++count);}

      Output:

      foo(1) calledfoo(2) calledfoo(3) calledfoo(4) called

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 7.13.1.1 The setjmp macro (p: 191)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.13.1.1 The setjmp macro (p: 262-263)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.13.1.1 The setjmp macro (p: 243-244)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.6.1 The setjmp macro

      [edit]See also

      jumps to specified location
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/program/setjmp&oldid=148503"

      [8]ページ先頭

      ©2009-2025 Movatter.jp