alloca(), #if, and other controversial things...

Peter da Silva peter at ficc.uu.net
Fri Aug 26 00:46:04 AEST 1988


The basic problem is that alloca doesn't handle unwinding anything but memory.
Here's a bare-bones exception handling system. Suitable macros can make it
as ADA-like as you can stand. For systems with decent signal handling (like,
I believe, BSD) wrap the critical sections in appropriate signal protection
code. Do this, and any errors will bubble up the call chain completely
safely.

struct jmp_buf error;

someroutine()
{
	struct jmp_buf old_error;
	struct foo *bar;
	int code;

	bar = 0;
	old_error = error;
	if(code = setjmp(error)) {
		if(bar) free(bar);
		error = old_error;
		longjmp(error, code);
	}
	bar = malloc(sizeof *bar);
	if(!bar) {
		error = old_error;
		longjmp(error, ENOMEM);
	}

	body of routine

	free(bar);
	error = old_error;
	return 0;
}
-- 
Peter da Silva  `-_-'  Ferranti International Controls Corporation.
"Have you hugged  U  your wolf today?"     sugar.uu.net!ficc!peter.



More information about the Comp.lang.c mailing list