doing nasty things with internal static variables

Doug Gwyn gwyn at smoke.brl.mil
Thu Mar 21 07:30:19 AEST 1991


In article <1991Mar19.183920.18911 at rice.edu> fontenot at comet.rice.edu (Dwayne Jacques Fontenot) writes:
-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).

There's nothing particularly wrong with that usage.  Objects having
static storage duration exist throughout program execution.  Now, if
you had omitted the "static" storage-class specifier, the array would
have been an automatic variable, and auto storage does "evaporate"
upon leaving the block in which it is declared.



More information about the Comp.lang.c mailing list