We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Dear all,
I have some problems with setjmp/ longjmp. I used the sample code (attached at the end). When I run my program the program steps into the trigger()-function and calls the longjump function. After this call I expect that the program steps back to the statement: if (setjmp (env) != 0) { but it does nothing and the program hangs in the longjump function. Do I have a wrong understanding of setjmp and longjmp?
Thanks for you help,
Christian
#include "setjmp.h" jmp_buf env; /* jump environment (must be global) */ int error_flag; void trigger (void) { /* processing code here */ if (error_flag != 0) { longjmp (env, 1); /* return 1 to setjmp */ } } void recover (void) { /* recovery code here */ } //----------------------------------------------------------------------------- // Main Program // int main(void) { if (setjmp (env) != 0) { /* setjmp returns a 0 */ printf ("LONGJMP called\n"); recover (); } else { printf ("SETJMP called\n"); error_flag = 1; /* force an error */ trigger (); } return 1; }