Using setjmp/longjmp

peter da silva peter at ficc.uu.net
Wed May 9 02:03:53 AEST 1990


> I think the poster meant that it's difficult to use longjmp() to abort
> to your program's main loop and at the same time clean up memory
> allocated "on the way down".

Rather than make this specific to longjmp, I tend to use a function
called "add_cleanup". This is passed a function name and a pointer to
a single argument, then when I want to clean things up I call
cleanup(), which pops all the functions off the stack and executes
them in turn.

For example:

	if(memory = malloc(...)) {
		add_cleanup(free, memory);
		...
	} else {
		errmsg = "Out of memory in foobar";
		longjmp(restart);
	}
-- 
`-_-' Peter da Silva. +1 713 274 5180.      <peter at ficc.uu.net>
 'U`  Have you hugged your wolf today?  <peter at sugar.hackercorp.com>
@FIN  Commercial solicitation *is* accepted by email to this address.



More information about the Comp.lang.c mailing list