Path: utzoo!yunexus!ists!helios.physics.utoronto.ca!news-server.csri.toronto.edu!mailrus!wuarchive!texbell!ficc!peter From: peter@ficc.ferranti.com (Peter da Silva) Newsgroups: comp.lang.c Subject: Re: free (NULL) Message-ID: <9YT3MP@xds13.ferranti.com> Date: 31 May 90 14:30:35 GMT Article-I.D.: xds13.9YT3MP References: <1771@mindlink.UUCP> <2574@skye.ed.ac.uk> <1074:May3000:24:1990@stealth.acf.nyu.edu> <3102@goanna.cs.rmit.oz.au> <3466:May3022:56:1890@stealth.acf.nyu.edu> Reply-To: peter@ficc.ferranti.com (Peter da Silva) Organization: Xenix Support, FICC Lines: 16 In article <3466:May3022:56:1890@stealth.acf.nyu.edu> brnstnd@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. 'U` Have you hugged your wolf today? @FIN Dirty words: Zhghnyyl erphefvir vayvar shapgvbaf.