[comp.lang.c] Wherefore is there setjmp

djones@megatest.UUCP (Dave Jones) (02/04/89)

I think I've used setjmp/longjmp twice in my life.

Setjmp is appropriate at a place in the code where there
might be a bunch of stuff on the runtime stack that turns out to be
useless.  You just want to discard it and get on with business.
Used that way, it is sort of a "structured jist-fergit-it."

An example would be a recursive descent command-line interpreter.
The user types stuff in, and the program recursively descends,
pushing things onto the stack. Somewhere deep in the descent, a
routine finds a non-recoverable syntax error. What to do?
Print a message, gobble input up to a "gobble-stopper", and
longjmp back to the outer layer of the parser.

A problem crops up if procedures along the way have allocated
memory from heap.  For applications which are very stackish in
nature, I have a "virtual stack" memory allocation package.
You can get a "mark" of the stack, and later pop back, recycling
all the memory above the mark.  Using this, you would mark the
memory-stack at the setjmp point, and pop it back to that state
after the longjmp.



			Dave Jones