Path: utzoo!attcan!uunet!sugar!ficc!peter From: peter@ficc.uu.net (Peter da Silva) Newsgroups: comp.lang.c Subject: Re: alloca(), #if, and other controversial things... Summary: You don't need alloca, you need exception handling! Message-ID: <1362@ficc.uu.net> Date: 25 Aug 88 14:46:04 GMT References: <8808171410.AA05337@ucbvax.Berkeley.EDU> <1259@garth.UUCP> <22725@amdcad.AMD.COM> Organization: SCADA Lines: 37 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.