What is the setjump call
Kee Hinckley
nazgul at apollo.uucp
Sat Sep 29 00:53:24 AEST 1984
<->
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.
Essentially it is just a way of implementing a non-local goto.
For example:
char[8] buf; /* Also sometimes known as type: jmp_buf */
foo()
{
/*
* Save state (when we return it will be as
* though we had just returned from the setjmp
* function.
*/
setjmp(buf);
bar();
...
}
bar()
{
...
longjmp(buf); /* Jump back into foo */
/* Nothing after this get's executed */
}
Note that the size of the buffer is going to be implementation dependent
(since different compilers are going to require different amounts of state
information) but generally it is on the order of a few bytes. Since
(at least on the Apple in Aztec C) all you have to store are the stack
pointers, the return vector, and a few other pieces of state. When
you restore all of these the rest of the stack gets thrown away and
you're right back where you started from.
One other thing, you can do a longjmp BACK to a setjmp, but doing one
forward probably won't work (eg. if you returned from "foo" in the above
example, and THEN tried to do a longjmp you would have to be very lucky
to have it work.
Actually, this is probably all a horrible explanation, does someone at
Brown have a graphical demo using Pecan or one of their other trees?
-kee
More information about the Comp.lang.c
mailing list