What is the setjump call

Bill Fecht bwf at ihldt.UUCP
Tue Oct 2 00:38:36 AEST 1984


Careful, the setjmp() call saves only a few registers and stack pointers,
it does not save the stack.  Not understanding this could cause two problems:

	1)  thinking that a longjmp() will restore automatic variables, i.e.

		#include <setjmp.h>
		
		jmp_buf jb;
		
		main()
		{
			int x;
			x = 1;
			setjmp(jb);
			if (x == 2) {
				printf("bailout\n");
				exit();
			}
			x = 2;
			longjmp(jb);
		}

	2)  thinking that a longjmp() will restore calling sequence, i.e.

		jmp_buf jb; 
		foo1() { foo2(); bar(); }
		foo2() { foo3(); }
		foo3() { setjmp(jb); }

		bar() { longjmp(jb); }

Also, you should use 'jmp_buf jb', NOT  'char[8] jb' (:-)), 'jmp_buf' will
reserve all you need to do the setjmp()/longjmp().


I think setjmp() and longjmp() were included in Unix to recover from
convoluted nesting in the event of errors, then were included in C for
the user, I think.  I know uucp uses the mechanism for this a lot.

bill (ihack!bwf) fecht



More information about the Comp.lang.c mailing list