Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!hsi!stpstn!lerman From: lerman@stpstn.UUCP (Ken Lerman) Newsgroups: comp.lang.c Subject: Re: Life after free? Keywords: free malloc Message-ID: <5620@stpstn.UUCP> Date: 28 Sep 90 12:00:52 GMT References: Reply-To: lerman@stpstn.UUCP (Ken Lerman) Distribution: comp Organization: The Stepstone Corporation, Sandy Hook, CT 06482 Lines: 32 In article quan@sol.surv.utas.oz (Stephen Quan) writes: ->char *funny(ch) ->char ch; ->{ -> char *tmp; -> int i; -> -> tmp = (char *) malloc(100); -> for (i=0; i<=99 ; i++) *(tmp+i) = ch; -> free(tmp); -> return tmp; ->} -> ->Any comments on free-ing tmp before it is return-ed? -> ->Stephen Quan (quan@sol.surv.utas.edu.au) ->University of Tasmania. Not only is it sensible, the semantics of free require it. The item freed is guaranteed to be valid until the next call to malloc, realloc, ... The intended use of funny() might be to format some characters which will be printed by the user immediately after calling funny. Unfortunately, the user can't call printf because printf might call malloc which would destroy his temporary buffer. So, yeah, it could be used. Is it useful (in the sense of full of use)? No way. Ken