Path: csiph.com!news.mixmin.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Libraries using longjmp for error handling Date: Sun, 01 Oct 2023 02:52:59 -0700 Organization: A noiseless patient Spider Lines: 29 Message-ID: <86il7qekjo.fsf@linuxsc.com> References: <65SQM.565977$9o89.411905@fx05.ams4> <871qejf62x.fsf@bsb.me.uk> <86y1gphiur.fsf@linuxsc.com> <877co9d8m2.fsf@bsb.me.uk> <86bkdlghqr.fsf@linuxsc.com> <20230929235846.a0883e001c83bbe718f5ab90@gmail.moc> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: dont-email.me; posting-host="5df80bfa77615176987b6091c1d3373e"; logging-data="1592466"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ktx8kGdEcggQ1Mm1bIz1827lq53hWLhM=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:IJpOCy1+qE7YmfKeUmRX0tIx34I= sha1:7ayo7tkOHqUDn2s/CK9loObKFm0= Xref: csiph.com comp.lang.c:176854 Anton Shepelev writes: > Tim Rentsch: > >> I wrote some code not long ago to add a value to a set of >> values, where the set is represented using a recursive >> binary tree structure (like red-black trees, but a little >> different). Usually we may expect that a request to add a >> value would offer a value not already included in the set, >> but certainly it can happen that there is a call to add a >> value that is already included, in which case the top >> level return value should be the original tree structure. > > Returning an error flag (or code) all the way up the call > stack may seem (and be) slow and cumbersome. If that case, > one can rewrite the recursive function iteratively, with an > explicit stack. I, however, think that an extra `if' in the > recursive function is so much less fuss Besides making the code slower and substantially longer, the resulting code would take a lot more effort to read and understand. In most cases lots of if()'s means the code is poorly written and should be re-thought. In this particular case re-writing the code to be iterative rather than recursive, and using an explicit stack, is a total non-starter. It's good to know the theoretical result that recursion can be turned into iteration, but in practice the two approaches are often not interchangeable.