What is the setjump call - (nf)

jim at ism780b.UUCP jim at ism780b.UUCP
Wed Oct 10 14:22:20 AEST 1984


#R:apollo:-22099700:ism780b:25500028:000:815
ism780b!jim    Oct  7 16:58:00 1984

#include <setjmp.h>

jmp_buf jmpbuf;

main()
{
	/* register */ int i;

	i = 0;
	setjmp(jmpbuf);
	for( ;; i++ )
	    foo(i);

	printf("%d\n", i);
}
foo(i)
{
	if( i == 5 ) longjmp(jmpbuf, 1);
}

If you get 5 from the above, but 0 if you remove the /* */,
your setjmp/longjmp is BROKEN!!  It should always give 5.
Note that the manual says that all data have values as of the time
*longjmp* was called, not as of the time the setjmp was called
(which is unimplementable; why do you get 5 instead of 0 in the above?).
To do this, longjmp must unwind the stack, restoring registers
as it goes.  Note that this only requires jmp_buf to have 3 words, instead
of the brain-damaged 10 in the 4.1 implementation, where the registers
are saved at setjmp time, which is all wrong.

-- Jim Balter, INTERACTIVE Systems (ima!jim)



More information about the Comp.lang.c mailing list