View Single Post
  #5   Spotlight this post!  
Unread 25-01-2007, 02:23
EricVanWyk EricVanWyk is offline
Registered User
no team
 
Join Date: Jan 2007
Rookie Year: 2000
Location: Boston
Posts: 1,597
EricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond reputeEricVanWyk has a reputation beyond repute
Send a message via AIM to EricVanWyk
Re: Why Static Variables?

Wow, I just got pwnt. Very nice post.

Can you link us to more information on overlay variables? I am unfamiliar with them.

The optimization I was referring to was the distinction between using the stack and not using the stack. Usually when one of my students proposed using a static variable, this was the purpose.

I think the issue was the difference between:
Code:
static int i;
i=0;
and
Code:
static int i=0;
I believe the first usage is where "overlay" is useful?

Sorry for any confusion. I've actually fixed more C code than I've written myself, so I'm more familiar with bad strategies than good ones.

In general, I'd force them to explain exactly why they want to use these static variables.
A counter internal to a function? Great.
An ill-informed attempt at scope-narrowing? Bad.
A well informed attempt at scope-narrowing? Good.
Writing a function and assuming it will always operate on a single set of data? NIGHTMARE. What happens if you suddenly grow another wheel/senesor/anything?

I do love "as narrow as possible" scoping, but I prefer placing variables with their buddies.

This is a paraphrased version of why I demand buddy variables share scope:
Code:
int softenMotor(int intended_value){
static int old_value=127;
int new_value = (old_value+intended_value);
old_value=new_value;
return new_value;
}
Oh goodness, the left and right motors are suddenly bound together through old_value.

I know I am being overly cranky. I know that no body would actually do that (more than twice). I am simply speaking as one who has burned himself with premature optimization time and time again.