This shows you the differences between two versions of the page.
c:setjmp.h:longjmp [2013/02/03 19:14] 127.0.0.1 external edit |
c:setjmp.h:longjmp [2024/02/16 01:05] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== longjmp ====== | ====== longjmp ====== | ||
<code c> | <code c> | ||
+ | void longjmp (jmp_buf env, int val); | ||
</code> | </code> | ||
==== description of longjmp ==== | ==== description of longjmp ==== | ||
- | longjmp is in work by code-reference.com \\ | + | get program state from the stack, useful for debugging. |
- | if you are faster... don't hasitate and add it | + | |
<code c> | <code c> | ||
- | no example at the moment | + | #include <stdio.h> |
+ | #include <setjmp.h> | ||
+ | |||
+ | void testit(int); | ||
+ | jmp_buf program_state; | ||
+ | |||
+ | static int count; | ||
+ | void testit(int count) { | ||
+ | |||
+ | count++; // set counter +1 but it will never reach 2 | ||
+ | printf("count is now %i\n", count); | ||
+ | } | ||
+ | |||
+ | int main(void) { | ||
+ | count = 0; | ||
+ | printf("count starts with: %i\n", count); | ||
+ | |||
+ | if(setjmp(program_state) == 0) { | ||
+ | printf("save program state in the stack\n"); | ||
+ | testit(count); | ||
+ | } | ||
+ | else { | ||
+ | printf("callback with longjmp\n"); | ||
+ | testit(count); | ||
+ | testit(count); | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | testit(count); | ||
+ | longjmp(program_state,1); | ||
+ | printf("never called\n"); | ||
+ | testit(count); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
</code> | </code> | ||
===== output of longjmp c example ===== | ===== output of longjmp c example ===== | ||
- | no example at the moment | + | count starts with: 0 |
+ | save program state in the stack | ||
+ | count is now 1 | ||
+ | count is now 1 | ||
+ | callback with longjmp | ||
+ | count is now 1 | ||
+ | count is now 1 | ||
+ |