This shows you the differences between two versions of the page.
c:setjmp.h:setjmp [2013/02/03 19:14] 127.0.0.1 external edit |
c:setjmp.h:setjmp [2024/02/16 01:05] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== setjmp ====== | ====== setjmp ====== | ||
<code c> | <code c> | ||
+ | int setjmp ( jmp_buf env ); | ||
</code> | </code> | ||
==== description of setjmp ==== | ==== description of setjmp ==== | ||
- | setjmp is in work by code-reference.com \\ | + | save program state / environment to the stack for a longjump 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 setjmp c example ===== | ===== output of setjmp 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 | ||
+ |