free (NULL)

Peter da Silva peter at ficc.ferranti.com
Fri Jun 1 00:30:35 AEST 1990


In article <3466:May3022:56:1890 at stealth.acf.nyu.edu> brnstnd at stealth.acf.nyu.edu (Dan Bernstein) writes:
> Namely: Whatever you allocate inside a routine, you also deallocate
> inside that routine. If your memory needs are variable, provide enough
> information to your callers that they can allocate for you. (This is
> called ``passing the buck.'')

I disagree with this entirely. If you return a variable amount of data to
your parent, there are two ways to go about it. First, you can do it like
"read", where you return multiple times (saving state between calls) until
all the information is returned. This is appropriate when the amount of
data you have to pass up is unbounded or at least large in comparison with
avaiable memory. Otherwise, returning a malloced chunk of memory is fine.
Generally, you should provide a routine to dispose of that memory, so you
can change the allocation strategy without letting the parent know (say,
you find that a single static array is enough, or you want to cache stuff
for efficiency). Fopen/fclose is an example of this type of interface.
-- 
`-_-' Peter da Silva. +1 713 274 5180.  <peter at ficc.ferranti.com>
 'U`  Have you hugged your wolf today?  <peter at sugar.hackercorp.com>
@FIN  Dirty words: Zhghnyyl erphefvir vayvar shapgvbaf.



More information about the Comp.lang.c mailing list