Usenet.com

www.Usenet.com

Group Index

Comp Thread Archive from Usenet.com

<-- __Chronological__ --> <-- __Thread__ -->

Re: C help please - C newbie



"Mark A. Odell" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Hans-Bernhard Broeker <[EMAIL PROTECTED]> wrote in
> news:[EMAIL PROTECTED]:
>
> >> > static variables have file level scope and typically presist
> >> > throughout operation.
> >
> >> Why typically? I believe the *must* persist for the duration of the
> >> program.
> >
> > Not if the compiler can determine that they're no longer needed after
> > some point in program flow.  They must behave *as if* they persisted
> > for the duration of the program --- but if the program doesn't look,
> > the compiler is allowed to re-use the space otherwise.
>
> I'd like to know if you have an example of such a condition as I'm having
> a hard time seeing how a compiler could know that it will swap out a
> function for use by another. Maybe I'm too used to Harvard architectures.

Observe.

static int switch = 0;
static int x;
static int y;

void test(void){
  if (switch == 0) {
    do a whole bunch of stuff with x
    if (some interesting case) {
      switch = 1;
    }
    do a bunch of stuff with y;
  } else {
    do a bunch of other stuff with y;
  }
}

Observe that the switch initially points to x, and the program uses x.  Once
the switch is thrown to y, the program only uses y, and NEVER AGAIN TOUCHES
X.  A sufficiently-advanced compiler could, in principle, notice that x and
y have disjoint lifetimes, and use the same storage location for the two
variables.





<-- __Chronological__ --> <-- __Thread__ -->


Usenet.com



Please check out one of the premium Usenet Newsgroup Service Providers below for access to Usenet.