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:
> 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. 

We were talking about variables, not functions.  But since you mention
it: code banking / overlaying works by essentially the same "as if"
rule.  From the abstract point of view of the compiled code, it looks
as if all functions were available at all times, when in fact they're
not.

But a compiler like Keil's C51, which already does static call tree
analysis anyway, could reasonably easily discover that function foo()
was only ever called a couple of times early on, and then never again.
Now, if there's a static-duration variable "bar" that's used only by
foo(), and another static-duration variable "bletch" that is not used
at all before the final call of foo() has terminated, then "bar" and
"bletch" _can_ be put in the same memory position, because there's no
way the code could know about this.

Whether or not a compiler actually attempts to exploit this
possibility is mainly a question of motivation.  It's absolutely
allowed by the language definition, but may not be applicable
frequently enough in real-world code to be worth the bother.  Clever
programmers may even find this optimization on their own, if pressed
for space.

The "as if" rule is simple: as long as the program behaves the same
way (in all aspects that the standard defines) _as if_ the
optimization hadn't taken place, the optimization is allowed.

> Maybe I'm too used to Harvard architectures. 

The architecture does not influence this much, if at all.

-- 
Hans-Bernhard Broeker ([EMAIL PROTECTED])
Even if all the snow were burnt, ashes would remain.



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


Usenet.com



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