This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

setjmp/ longjmp

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;
}