[vox-tech] C question: global vs local const

Mark K. Kim vox-tech@lists.lugod.org
Fri, 18 Jan 2002 08:25:58 -0800 (PST)


Keywords: global, local, c, variables, const

On Fri, 18 Jan 2002, Peter Jay Salzman wrote:

> does this apply to static variables too?  i have a situation where a
> function is called many times over:
>
> void function( ..., long double dr)
> {
> 	long double variable = expensive_calculation * dr;
> 	...
> }
>
> i'd like to declare variable as static, since both expensive_calculation
> and dr remain constant through the entire program.  i can't declare dr
> as being global because it depends on other parameters that need to be
> calculated at run time (but otherwise don't change).

Hmmm... ummm... hmmm...  Think, think...

Yes.  If they aren't calculated at compile time, then you need some way to
tell whether a function call during runtime is the "first call".  This
would have to be something like:

   int function(int arg)
   {
      if(first_call())
      {
         /* initialize static variables */
      }

      blah blah...
   }

This adds hidden overhead to a function call, which would be a no-no for C.

-Mark (making it up as he goes...)

--
Mark K. Kim
http://www.cbreak.org/mark/
PGP key available upon request.