Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>setjmp is used to place a <em>marker</em> to where the call of longjump should return, it returns 0 if it is called directly, it returns 1 if it's called because a longjmp to that setjmp is invoked.</p> <p>You have to think about setjmp like something that can be normally called and does not do anything (returning 0) in normal operation while returns 1 and it's indirectly called (and returns from there) when a long jump is called. I know what you mean about confusing because it's actually confusing..</p> <p>This is the example given by wikipedia:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;setjmp.h&gt; static jmp_buf buf; void second(void) { printf("second\n"); // prints longjmp(buf,1); // jumps back to where setjmp was called - making setjmp now return 1 } void first(void) { second(); printf("first\n"); // does not print } int main() { if ( ! setjmp(buf) ) { first(); // when executed, setjmp returns 0 } else { // when longjmp jumps back, setjmp returns 1 printf("main"); // prints } return 0; } </code></pre> <p>Are you able to understand it? When program is launched <code>setjmp</code> is executed in main and returns 0 (because it is directly called), so <code>first</code> is called, that calls <code>second</code> and then it arrives <code>longjmp</code> that switches context going back to where <code>setjmp</code> was used, but this time, since it goes back from a jump and it is indirectly called the function returns 1.</p> <p>The useful thing of setjmp/longjmp approach is that you can handle error situations without caring to keep a flag between function calls (especially when you have many, think about a recursive procedure for typechecking in a compiler). If something goes wrong in typechecking deep in call stack normally you have to return a flag and keep returning it to warn the caller that typechecking failed. With <strong>longjmp</strong> you just go out and handle error without caring about passing flags back. The only problem is that this forces a context switch that doesn't care about standard deallocation of stack/heap memory so you should handle it by yourself.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload