Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!purdue!haven!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn) Newsgroups: comp.lang.c Subject: Re: Oh noooooo!! Message-ID: <10972@smoke.BRL.MIL> Date: 7 Sep 89 20:08:27 GMT References: <7598@goofy.megatest.UUCP> <34566@apple.Apple.COM> Reply-To: gwyn@brl.arpa (Doug Gwyn) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 33 In article <34566@apple.Apple.COM> ftanaka@Apple.COM (Forrest Tanaka) writes: >I've been using gotos regularly in my C code for quite a few months--in one >specific situation. That situation is for error handling. Your example could be generalized as follows: char * SomeFunction () { char *Block0; char *Block1; char *Block2; if ((Block0 = AllocateMemory ()) == NULL) goto Abort0; if ((Block1 = AllocateMemory ()) == NULL) goto Abort1; if ((Block2 = AllocateMemory ()) == NULL) goto Abort2; return Block0; Abort2: DeallocateMemory (Block1); Abort1: DeallocateMemory (Block0); Abort0: return (char *) null; } which we ended up adopting extensively in the huge programming project I occasionally mention. It brings a high degree of order to the error handling process, significantly improving the chances that all actions get unwound properly on an abort.