Steve Rosen's setjmp problem

Andrew Koenig ark at alice.UUCP
Fri Jun 7 14:20:16 AEST 1985


Steve Rosen complains that the following C program fails:

#include <setjmp.h>

static jmp_buf env;
int mode;

main(){
	foo();
	longjmp(env, 1);
}

foo(){

  mode = setjmp(env);
  if (mode != 0) magic();
}

magic()
{
	printf("HERE I AM\n");
	exit(0);
}

The reason the program fails is that foo returns after calling
setjmp.  This destroys the environment that setjmp saved,
so there is nothing for longjmp to return to.  General rule:
every call to longjmp must be made from a function that
is a dynamic descendent of the one that made the corresponding
call to setjmp.



More information about the Comp.unix.wizards mailing list