Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Anton Shepelev Newsgroups: comp.lang.c Subject: Re: Fixing a sample from K&R book using cake static analyser Date: Mon, 24 Jun 2024 01:59:40 +0300 Organization: A noiseless patient Spider Lines: 40 Message-ID: <20240624015940.be2546d45cc185a330bdee55@gmail.moc> References: <20240623022343.ec355c69a3d9eb03ad4a50f2@gmail.moc> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Injection-Date: Mon, 24 Jun 2024 00:59:41 +0200 (CEST) Injection-Info: dont-email.me; posting-host="c092b4272f57de9975d74d7342d26349"; logging-data="595713"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/mxhX7jYTiIgOb/HWLze9jICuWUgCcsQs=" Cancel-Lock: sha1:+qM/x7L3ZZSLlIoGIz2qnzHmk74= X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32) Bytes: 2413 Lawrence D'Oliveiro to Anton Shepelev: > > Why are you so afraid of `goto' ... > > Look up the concept of a "Nassi-Shneiderman diagram". It > allows arbitrary nesting of complex code, with dynamic > allocation going on, while minimizing flow-control > headaches. Thank you, I will: http://www.cs.umd.edu/hcil/members/bshneiderman/nsd/1973.pdf I hate the traditional flowcharts, and the N.-S. certainly look so much better. > And while I'm at it, it makes sense to factor out the > table entry disposal code into a separate routine. > > void np_free(struct nlist *np) > { > if (np != NULL) > { > free(np->name); > free(np->defn); > } /*if*/ > free(np); > } /*np_free*/ I thought the challenge was to fix it as a single function. np_free() helps, but is a tad redundant in that it always tried to dispose of the whole thing, even when at calling point we know for certain not all three object have been allocted. You further simplify things by zero-filling via calloc() and relying on free() accepting NULL pointers, whereas I prefere to avoid such redundant calls of free(). -- () ascii ribbon campaign -- against html e-mail /\ www.asciiribbon.org -- against proprietary attachments