Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!yale!mintaka!spdcc!ima!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Why 'life after free'. Keywords: question about malloc+free combination. Message-ID: <18363@haddock.ima.isc.com> Date: 2 Oct 90 02:20:59 GMT References: Reply-To: karl@kelp.ima.isc.com (Karl Heuer) Distribution: comp Organization: Interactive Systems, Cambridge, MA 02138-5302 Lines: 19 In article quan@sol.surv.utas.oz (Stephen Quan) writes: >I normally do what you suggest, the reason why a brought up this issue is >that if what I propose wasn't so unpredictable then I can have something >like : > printf("%s\n",funny('c')); > printf("%s\n",funny('x')); Bad idea, even under the old rules. This depends on the funny-buffer retaining its contents until printf() copies it into the stdio buffer. So it will break if malloc() is called by printf() or any of the stdio routines it calls--which is sometimes the case. I generally recommend temp = funny('c'); printf("%s\n", temp); free(temp); instead. If you have enough of these that it's painful, consider writing your own allocation package on top of malloc()/free(), and include a function that will garbage collect the temporaries. Shouldn't be difficult. Karl W. Z. Heuer (karl@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint