doing nasty things with internal static variables

Chris Torek torek at elf.ee.lbl.gov
Thu Mar 21 06:09:08 AEST 1991


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

Consider yourself corrected.

The *entire* difference between a local `static' and a local automatic
variable is that there is, at *all* times that the program runs, exactly
one instance of the `static', while there may be zero or more instances
of the automatic (and in fact the number of instances is exactly the
same as the number of times that automatic is in scope, i.e.,

	f(n) { int silly; if (n > 1) f(n - 1); }
	...
		f(50);

has 50 copies of `silly' floating around somewhere inside the machine%
at the deepest level of recursion).
-----
% That is, provided the optimizer has not deleted this unused variable.
  The variables are not necessarily `on a stack'; the machine may not
  even have `stacks'.  Stacks are simply a common implementation method.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.lang.c mailing list