What is the setjump call

Brian Thomson thomson at uthub.UUCP
Tue Oct 2 01:45:20 AEST 1984


Kee Hinckley almost gets it right:
>     Setjump stores the current state of the stack and any relevant
> registers and hands them back to the program to hang onto.  At some
> later date the program can then call longjmp and everything will
> return to the state of the machine at the time of the setjump.

In fact, a correct setjmp/longjmp implementation doesn't just store
and restore register contents.  Rather, longjmp() should do a 'deep return',
restoring registers only if they have been salted away as part of
normal program execution.  In particular, the program

jmp_buf env;

main()
{
	register int i;

	i = 0;
	if(setjmp(&env)) {
		printf("%d\n", i);
		exit(0);
	}
	i = 1;
	longjmp(&env, 1);
}

should print 1, not 0.  Check the manual page -- ours (4.2) says
"All accessible data have values as of the time longjmp was called."
Note that, with this definition, the behaviour of a variable is independent
of whether it was declared "register".
-- 
		    Brian Thomson,	    CSRI Univ. of Toronto
		    {linus,ihnp4,uw-beaver,floyd,utzoo}!utcsrgv!uthub!thomson



More information about the Comp.lang.c mailing list