Sujet : Re: Fixing a sample from K&R book using cake static analyser
De : anton.txt (at) *nospam* gmail.moc (Anton Shepelev)
Groupes : comp.lang.cDate : 23. Jun 2024, 23:59:40
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240624015940.be2546d45cc185a330bdee55@gmail.moc>
References : 1 2 3 4
User-Agent : Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
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.pdfI 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