Life after free?

Norman Joseph norm at oglvee.UUCP
Fri Sep 28 22:59:11 AEST 1990


In <quan.654410256 at sol> quan at sol.surv.utas.oz (Stephen Quan) writes:

>char *funny(ch)
>char ch;
>{
>  char *tmp;
>  [...]
>  return tmp;
>}

>Any comments on free-ing tmp before it is return-ed?

No, but I -do- have a comment on returning tmp at all.  The storage
class in the declaration of tmp defaults to "auto".  This gives the
variable tmp a number of important properties, one of which is dynamic
duration.  Dynamic duration means that tmp only "exists" while the
function in which it is declared is executing.  After returning from
the function, there is no guarantee about the value of the now non-
existent variable tmp.

If you want the variable tmp to retain its value between calls to the
function, declare the variable as "static char *tmp".
-- 
Norm Joseph - (amanue!oglvee!norm)           cgh!amanue!oglvee!norm at dsi.com, or
  Oglevee Computer Systems, Inc.             ditka!oglvee!norm at daver.bungi.com
                                   ---<*>---
      "Shucking Usenet oysters in pursuit of a pearl."  --  Bill Kennedy



More information about the Comp.lang.c mailing list