Path: utzoo!attcan!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!usc!snorkelwacker!bloom-beacon!eru!hagbard!sunic!mcsun!ukc!slxsys!ibmpcug!dylan From: dylan@ibmpcug.co.uk (Matthew Farwell) Newsgroups: comp.lang.c Subject: Re: Why 'life after free'. Keywords: question about malloc+free combination. Message-ID: <1990Sep30.163824.12974@ibmpcug.co.uk> Date: 30 Sep 90 16:38:24 GMT References: Reply-To: dylan@ibmpcug.CO.UK (Matthew Farwell) Distribution: comp Organization: The IBM PC User Group, UK. Lines: 38 In article quan@sol.surv.utas.oz (Stephen Quan) writes: > From: wuxing@comp.mscs.mu.edu (Xing Wu) > >In article you write: > >> tmp = (char *) malloc(100); > >> for (i=0; i<=99 ; i++) *(tmp+i) = ch; > >> free(tmp); > >> return tmp; > 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')); > > Where funny will create a string of 100 c's or 100 s's. The string is > displayed in 'printf' (hopefully) and you don't need to worry about > free-ing the memory. Forgive me if I'm wrong, but whats wrong with doing .... char * funny(c) char c; { static char a[100]; int i; for (i=0 ; i<99 ; i++) a[i] = c; a[99] = '\0'; return (&a[0]); } Dylan. -- Matthew J Farwell | Email: dylan@ibmpcug.co.uk The IBM PC User Group, PO Box 360,| ...!uunet!ukc!ibmpcug!dylan Harrow HA1 4LQ England | CONNECT - Usenet Access in the UK!! Phone: +44 81-863-1191 | Sun? Don't they make coffee machines?