Why 'life after free'.
michelle krill-woman lee
mikey at ontek.com
Fri Oct 5 10:51:34 AEST 1990
| > 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]);
| > }
| >
|
| With this function call's like
| printf( "%s %s\n", funny( 'x' ), funny( 'y' ) );
| do not work as expected.
This sort of thing wouldn't work with malloc/free as originally
proposed either. If funny freed the block its return pointer pointed
at, the second invocation (not necessarily the one for y, btw) might
cause malloc to stomp on the thing that was just freed, before printf
even has a chance to print it. It's either that or don't free it and
get a memory leak. Better solutions have been mentioned elsewhere in
this thread of articles, i.e. using variables in the caller to store
the result.
the krill, i/o bound
More information about the Comp.lang.c
mailing list