Why 'life after free'.
Matthew Farwell
dylan at ibmpcug.co.uk
Mon Oct 1 02:38:24 AEST 1990
In article <quan.654673946 at sol> quan at sol.surv.utas.oz (Stephen Quan) writes:
> From: wuxing at comp.mscs.mu.edu (Xing Wu)
> >In article <quan.654410256 at sol> 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 at 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?
More information about the Comp.lang.c
mailing list