diff options
Diffstat (limited to 'devdocs/c/program%2Flongjmp.html')
| -rw-r--r-- | devdocs/c/program%2Flongjmp.html | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/devdocs/c/program%2Flongjmp.html b/devdocs/c/program%2Flongjmp.html new file mode 100644 index 00000000..2ccde1dd --- /dev/null +++ b/devdocs/c/program%2Flongjmp.html @@ -0,0 +1,64 @@ + <h1 id="firstHeading" class="firstHeading">longjmp</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><setjmp.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-until-c11"> <td> <pre data-language="c">void longjmp( jmp_buf env, int status );</pre> +</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-until-c11">(until C11)</span> </td> </tr> <tr class="t-dcl t-since-c11 t-until-c23"> <td> <pre data-language="c">_Noreturn void longjmp( jmp_buf env, int status );</pre> +</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> <br><span class="t-mark-rev t-until-c23">(until C23)</span> </td> </tr> <tr class="t-dcl t-since-c23"> <td> <pre data-language="c">[[noreturn]] void longjmp( jmp_buf env, int status );</pre> +</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> </tr> </table> <p>Loads the execution context <code>env</code> saved by a previous call to <code><a href="setjmp" title="c/program/setjmp">setjmp</a></code>. This function does not return. Control is transferred to the call site of the macro <code><a href="setjmp" title="c/program/setjmp">setjmp</a></code> that set up <code>env</code>. That <code><a href="setjmp" title="c/program/setjmp">setjmp</a></code> then returns the value, passed as the <code>status</code>.</p> +<p>If the function that called <code><a href="setjmp" title="c/program/setjmp">setjmp</a></code> has exited (whether by return or by a different <code>longjmp</code> higher up the stack), the behavior is undefined. In other words, only long jumps up the call stack are allowed.</p> +<table class="t-rev-begin"> <tr class="t-rev t-since-c11"> +<td> <p>Jumping across threads (if the function that called <code>setjmp</code> was executed by another thread) is also undefined behavior.</p> +</td> <td><span class="t-mark-rev t-since-c11">(since C11)</span></td> +</tr> </table> <table class="t-rev-begin"> <tr class="t-rev t-since-c99"> +<td> <p>If when <code><a href="setjmp" title="c/program/setjmp">setjmp</a></code> was called, a <a href="../language/array" title="c/language/array">VLA</a> or another <a href="../language/declarations" title="c/language/declarations">variably-modified type</a> variable was in scope and control left that scope, <code>longjmp</code> to that <code>setjmp</code> invokes undefined behavior even if control remained within the function.</p> +<p>On the way up the stack, <code>longjmp</code> does not deallocate any VLAs, memory leaks may occur if their lifetimes are terminated in this way:</p> +<div class="c source-c"><pre data-language="c">void g(int n) +{ + int a[n]; // a may remain allocated + h(n); // does not return +} +void h(int n) +{ + int b[n]; // b may remain allocated + longjmp(buf, 2); // might cause a memory leak for h's b and g's a +}</pre></div> </td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td> +</tr> </table> <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> env </td> <td> - </td> <td> variable referring to the execution state of the program saved by <code><a href="setjmp" title="c/program/setjmp">setjmp</a></code> </td> +</tr> <tr class="t-par"> <td> status </td> <td> - </td> <td> the value to return from <code><a href="setjmp" title="c/program/setjmp">setjmp</a></code>. If it is equal to <code>0</code>, <code>1</code> is used instead </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <p>(none)</p> +<h3 id="Notes"> Notes</h3> <p><code>longjmp</code> is intended for handling unexpected error conditions where the function cannot return meaningfully. This is similar to exception handling in other programming languages.</p> +<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stdio.h> +#include <setjmp.h> +#include <stdnoreturn.h> + +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.2.1 The longjmp macro (p: 191-192) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.13.2.1 The longjmp macro (p: 263-264) </li></ul> +<li> C99 standard (ISO/IEC 9899:1999): </li> +<ul><li> 7.13.2.1 The longjmp macro (p: 244-245) </li></ul> +<li> C89/C90 standard (ISO/IEC 9899:1990): </li> +<ul><li> 4.6.2.1 The longjmp function </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="setjmp" title="c/program/setjmp"> <span class="t-lines"><span>setjmp</span></span></a></div> </td> <td> saves the context <br> <span class="t-mark">(function macro)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/utility/program/longjmp" title="cpp/utility/program/longjmp">C++ documentation</a></span> for <code>longjmp</code> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/program/longjmp" class="_attribution-link">https://en.cppreference.com/w/c/program/longjmp</a> + </p> +</div> |
