summaryrefslogtreecommitdiff
path: root/devdocs/c/program%2Fsetjmp.html
blob: b0fedd150762c375d2d90efa84c865468624062d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    <h1 id="firstHeading" class="firstHeading">setjmp</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;setjmp.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">#define setjmp(env) /* implementation-defined */</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr>  </table> <p>Saves the current execution context into a variable <code>env</code> of type <code><a href="jmp_buf" title="c/program/jmp buf">jmp_buf</a></code>. This variable can later be used to restore the current execution context by <code><a href="longjmp" title="c/program/longjmp">longjmp</a></code> function. That is, when a call to <code><a href="longjmp" title="c/program/longjmp">longjmp</a></code> function is made, the execution continues at the particular call site that constructed the <code><a href="jmp_buf" title="c/program/jmp buf">jmp_buf</a></code> variable passed to <code><a href="longjmp" title="c/program/longjmp">longjmp</a></code>. In that case <code>setjmp</code> returns the value passed to <code><a href="longjmp" title="c/program/longjmp">longjmp</a></code>.</p>
<p>The invocation of <code>setjmp</code> must appear only in one of the following contexts:</p>
<ol>
<li> The entire controlling expression of <a href="../language/if" title="c/language/if"><code>if</code></a>, <a href="../language/switch" title="c/language/switch"><code>switch</code></a>, <a href="../language/while" title="c/language/while"><code>while</code></a>, <a href="../language/do" title="c/language/do"><code>do-while</code></a>, <a href="../language/for" title="c/language/for"><code>for</code></a>.<div class="c source-c"><pre data-language="c">switch(setjmp(env)) { // ...</pre></div> </li>
<li> 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 of <a href="../language/if" title="c/language/if"><code>if</code></a>, <a href="../language/switch" title="c/language/switch"><code>switch</code></a>, <a href="../language/while" title="c/language/while"><code>while</code></a>, <a href="../language/do" title="c/language/do"><code>do-while</code></a>, <a href="../language/for" title="c/language/for"><code>for</code></a>.<div class="c source-c"><pre data-language="c">if(setjmp(env) &gt; 10) { // ...</pre></div> </li>
<li> The operand of a unary ! operator with the resulting expression being the entire controlling expression of <a href="../language/if" title="c/language/if"><code>if</code></a>, <a href="../language/switch" title="c/language/switch"><code>switch</code></a>, <a href="../language/while" title="c/language/while"><code>while</code></a>, <a href="../language/do" title="c/language/do"><code>do-while</code></a>, <a href="../language/for" title="c/language/for"><code>for</code></a>.<div class="c source-c"><pre data-language="c">while(!setjmp(env)) { // ...</pre></div> </li>
<li> The entire expression of an <a href="../language/statements#Expression_statements" title="c/language/statements">expression statement</a> (possibly cast to <code>void</code>).<div class="c source-c"><pre data-language="c">setjmp(env);</pre></div> </li>
</ol> <p>If <code>setjmp</code> appears in any other context, the behavior is undefined.</p>
<p>Upon return to the scope of <code>setjmp</code>:</p>
<ul>
<li> all accessible objects, floating-point status flags, and other components of the abstract machine have the same values as they had when <code><a href="longjmp" title="c/program/longjmp">longjmp</a></code> was executed, </li>
<li> except for the non-<a href="../language/volatile" title="c/language/volatile"><code>volatile</code></a> local variables in the function containing the invocation of <code>setjmp</code>, whose values are indeterminate if they have been changed since the <code>setjmp</code> invocation. </li>
</ul>  <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> env </td> <td> - </td> <td> variable to save the execution state of the program to. </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p><code>​0​</code> if the macro was called by the original code and the execution context was saved to <code>env</code>.</p>
<p>Non-zero value if a non-local jump was just performed. The return value is the same as passed to <code><a href="longjmp" title="c/program/longjmp">longjmp</a></code>.</p>
<h3 id="Notes"> Notes</h3> <p>Above requirements forbid using return value of <code>setjmp</code> 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.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;setjmp.h&gt;
#include &lt;stdnoreturn.h&gt;
 
jmp_buf my_jump_buffer;
 
noreturn void 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)
{
    volatile int count = 0; // modified local vars in setjmp scope must be volatile
    if (setjmp(my_jump_buffer) != 5) // compare against constant in an if
        foo(++count);
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">foo(1) called
foo(2) called
foo(3) called
foo(4) called</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.13.1.1 The setjmp macro (p: 191) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.13.1.1 The setjmp macro (p: 262-263) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.13.1.1 The setjmp macro (p: 243-244) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.6.1 The setjmp macro </li></ul>
</ul>               <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="longjmp" title="c/program/longjmp"> <span class="t-lines"><span>longjmp</span></span></a></div> </td> <td> jumps to specified location <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/utility/program/setjmp" title="cpp/utility/program/setjmp">C++ documentation</a></span> for <code>setjmp</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/program/setjmp" class="_attribution-link">https://en.cppreference.com/w/c/program/setjmp</a>
  </p>
</div>