Life after free?

Ken Lerman lerman at stpstn.UUCP
Fri Sep 28 22:00:52 AEST 1990


In article <quan.654410256 at sol> quan at 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?
->
->Stephen Quan (quan at sol.surv.utas.edu.au)
->University of Tasmania.


Not only is it sensible, the semantics of free require it.  The item
freed is guaranteed to be valid until the next call to malloc,
realloc, ...

The intended use of funny() might be to format some characters which
will be printed by the user immediately after calling funny.
Unfortunately, the user can't call printf because printf might call
malloc which would destroy his temporary buffer.

So, yeah, it could be used.  Is it useful (in the sense of full of
use)? No way.

Ken



More information about the Comp.lang.c mailing list