Path: utzoo!attcan!uunet!samsung!munnari.oz.au!csc.anu.oz.au!csc3.anu.oz.au!csc.canberra.edu.au!news From: eyal@delta.canberra.edu.au (Eyal Lebedinsky) Newsgroups: comp.lang.c Subject: Re: Life after free? Keywords: free malloc Message-ID: <1990Sep30.002157.7688@csc.canberra.edu.au> Date: 30 Sep 90 00:21:57 GMT References: Sender: news@csc.canberra.edu.au Distribution: comp Organization: none Lines: 29 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? I hope this is a serious question. You should not use an area after it is freed. Not only can it be re-used, but some schemes clobber it when it is added to the free-list (that is, they use some of it for the memory management). Remember that some C functions allocate memory implicitly, so you cannot trust your memory just because you do no mallocs after the free(). > >Stephen Quan (quan@sol.surv.utas.edu.au) >University of Tasmania. Regards Eyal -- Regards Eyal