doing nasty things with internal static variables

Stan Brown browns at iccgcc.decnet.ab.com
Fri Mar 22 03:58:14 AEST 1991


In article <1991Mar19.183920.18911 at rice.edu>, fontenot at rice.edu (Dwayne Jacques Fontenot) writes:
> I have found myself doing this because it works, but I am curious if it
> is a common practice or if it is highly likely to get me into trouble:
> 
> char *foo()
> {
>   static char string[64];
>   ...
>   return(string);
> }
> 
> I am concerned about this because though I know that that static variable
> is guaranteed to always be there for the function containing it, it is
> not really guaranteed to be there (in memory) at any other time
> (correct me if I'm wrong).

This is one time where you might be glad to be wrong.

Any variable declared as static is guaranteed to stay around for the
entire execution of the program.

BTW, the parentheses in your return statement are not necessary.

My opinions are mine:  I don't speak for any other person or company.
                   email (until 91/4/30): browns at iccgcc.decnet.ab.com
Stan Brown, Oak Road Systems, Cleveland, Ohio, USA    +1 216 371 0043



More information about the Comp.lang.c mailing list