Why 'life after free'.
Karl Heuer
karl at haddock.ima.isc.com
Tue Oct 2 12:20:59 AEST 1990
In article <quan.654673946 at sol> quan at sol.surv.utas.oz (Stephen Quan) writes:
>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'));
Bad idea, even under the old rules. This depends on the funny-buffer
retaining its contents until printf() copies it into the stdio buffer.
So it will break if malloc() is called by printf() or any of the stdio
routines it calls--which is sometimes the case.
I generally recommend
temp = funny('c'); printf("%s\n", temp); free(temp);
instead. If you have enough of these that it's painful, consider writing
your own allocation package on top of malloc()/free(), and include a function
that will garbage collect the temporaries. Shouldn't be difficult.
Karl W. Z. Heuer (karl at kelp.ima.isc.com or ima!kelp!karl), The Walking Lint
More information about the Comp.lang.c
mailing list