|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
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; Code:
static int i=0; 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;
}
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. |
|
#2
|
|||
|
|||
|
Re: Why Static Variables?
Quote:
Write file modules around functions that work on one structure type. This allows you to mimic some of the good organization that comes with using C++ classes in C. File scope static variables are equivalent to class static variables. Static functions are equivalent to protected or private C++ functions. Only expose the public interface to struct with the global functions. This keeps things pretty encapsulated. You can preface all the global functions with a name related to the structure they work on to minimize name clashes. |
|
#3
|
||||
|
||||
|
Re: Why Static Variables?
Quote:
All the information I gave you was from the C18 Compiler manual, specifically page 20 of the User's Guide PDF on Kevin's site. (This is actually page 12 of the manual if you happen to have it printed out.) One of the the key issues with overlay is that any sort of recursion is impossible. (e.g., you couldn't use it in the typical factorial implementation). You will be given an error by the linker if this is the case though, so you shouldn't have to worry about random unknown errors popping up because of this. The example you gave later in your post is correct (assuming I am inferring your intentions correctly). A static variable is used when you want the value of the variable to stay the same between function calls. An overlay or auto variable is used when you do not want that to be the case. Quote:
Quote:
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Static Cling?? | orelinde | Electrical | 17 | 27-02-2006 17:33 |
| Static Variables | amateurrobotguy | Programming | 8 | 04-03-2005 00:40 |
| Static Electricity | JeffO | Championship Event | 5 | 19-04-2004 11:04 |
| Static on the HDPE | Al Skierkiewicz | Technical Discussion | 60 | 12-03-2003 17:52 |
| Static Electricity | archiver | 2001 | 12 | 24-06-2002 00:34 |