I am using setjmp and longjmp for the first time, and I run into a problem that occurs when I wrap setjmp and longjmp. I threw the code back to the following example:
#include <stdio.h> #include <setjmp.h> jmp_buf jb; int mywrap_save() { int i = setjmp(jb); return i; } int mywrap_call() { longjmp(jb, 1); printf("this shouldn't appear\n"); } void example_wrap() { if (mywrap_save() == 0){ printf("wrap: try block\n"); mywrap_call(); } else { printf("wrap: catch block\n"); } } void example_non_wrap() { if (setjmp(jb) == 0){ printf("non_wrap: try block\n"); longjmp(jb, 1); } else { printf("non_wrap: catch block\n"); } } int main() { example_wrap(); example_non_wrap(); }
Initially, I thought that example_wrap () and example_non_wrap () would behave the same. However, the result of running the program (GCC 4.4, Linux):
wrap: try block non_wrap: try block non_wrap: catch block
If I spend the program in gdb, I see that although mywrap_save () returns 1, the else branch after the return is strangely ignored. Can anyone explain what is happening?
source share