From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fac41,9a0ff0bffdf63657 X-Google-Attributes: gidfac41,public X-Google-Thread: f43e6,9a0ff0bffdf63657 X-Google-Attributes: gidf43e6,public X-Google-Thread: 1108a1,9a0ff0bffdf63657 X-Google-Attributes: gid1108a1,public X-Google-Thread: 103376,4b06f8f15f01a568 X-Google-Attributes: gid103376,public From: "Mattias Lundstr�m" Subject: Re: Software landmines (loops) Date: 1998/09/02 Message-ID: <35ED4937.1D01ED67@ehpt.com>#1/1 X-Deja-AN: 387125751 Content-Transfer-Encoding: 7bit References: <902934874.2099.0.nnrp-10.c246a717@news.demon.co.uk> <6r1glm$bvh$1@nnrp1.dejanews.com> <6r9f8h$jtm$1@nnrp1.dejanews.com> <6renh8$ga7$1@nnrp1.dejanews.com> <6rf59b$2ud$1@nnrp1.dejanews.com> <6rfra4$rul$1@nnrp1.dejanews.com> <35DBDD24.D003404D@calfp.co.uk> <6sbuod$fra$1@hirame.wwa.com> <35f51e53.48044143@ <904556531.666222@miso.it.uq.edu.au> <35EAB5B1.1DA1986B@ehpt.com> <6sf1dn$n52$1@hirame.wwa.com> <35EB9B91.3FF8583@ehpt.com> <6sh2rp$8v3$1@hirame.wwa.com> Organization: Ericsson HP X-Priority: 3 (Normal) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 Newsgroups: comp.lang.eiffel,comp.object,comp.software-eng,comp.lang.ada Date: 1998-09-02T00:00:00+00:00 List-Id: Aggresive cuttings below. Robert Martin wrote: > [...] > This is roughly equivalent to the 'finally' solution in another post. And > I'll use the same argument. [...] [Loop that sets return value 'retval' and does 'goto error_exit' on error] > error_exit: > if (p) free(p); > if (f) fclose(f); return retval; // The point is that this is code that is ALWAYS executed. // The only "multi-exit" issue here is the multiple exit // points from the loop. The function has only this exit. Your problem with this is (as I understand you) is the use of flags (or other method of tracking) to be able to say if( resource1 allocated ) deallocate( resource1 ); if( resource2 allocated ) deallocate( resource2 ); ... if( resourceN allocated ) deallocate( resourceN ); How do you mean that you could avoid this by a single-exit solution ? > > The redundant resource cleanup, and the checks made in the error_exit > section are pretty ugly. And this ugliness will only grow and become more Perhaps. But I do think they are unavoidable. You may break them up over multiple functions when and if they become too complex, but this has really nothing to do with the single/multi-exit issue. > contorted over time. Also, it is not guaranteed that all resources will > have check functions, forcing the use of flags (which is all p and f are > used for in error_exit) in order to determine when it is safe to release > such a resource. > > Single-entry/single-exit (se/se) structures always have appropriate places > for acuiring and freeing resources. The state of the algorithm is encoded > into the structure. Multiple exits, gotos, finally clauses, destructures, > etc, all *lose* state information. Once they are invoked, the previous > state of the algorithm can only be recaptured by investigating the data and > within the algorithm. This means flags that cover the entire scope of the > algorithm must be maintained so that the error recovery code can determine > what to do. The whole point of having resource deallocation at the end of the loop is that you do not want to distribute this into many places. Are you (I hope not) proposing something like while( loop should continue ) { ... if( error 1 ) { do some deallocation set flag(s) not to continue } ... if( error 2 ) { do some oother (possibly overlapping) deallocation set flag(s) not to continue } ... } > The decision to use multiple exists (other than exceptions) is a short term > decision that will probably yeild short term benefits. But in the long term > the decision has the potential to cause significant pain. Personally, I > have experienced enough of that sort of pain, so I am pretty careful about > se/se. As can be deduced from the above I do not agree with you ;-) Btw. Why, in your opinion, are exceptions different? - Mattias