|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Re: Why Static Variables?
Quote:
Global variables have program scope meaning they can be seen anywhere in the entire program provided a declaration is visible. Static variables can have file, function, or even block scope. Automatic variables can only have function and block scope by contrast. The visibility of heap variables is controlled by the visibility of the pointer variables which, well, point to them. Good programming practices usually suggest that variables be given the smallest scope to do the job. This prevents accidental access via things like mispelling the variable name. It also limits name clashes for commonly used variable names and makes the logic easier to follow. That said, most programmers don't use block scope in C and the smallest common scope is the function. Using more descriptive names helps with name clashes but it's nice to be able to use a simple descriptive name without having to worry too much about it until the linker complains. Some coding styles preface global variables with "g_" and file scope statics with "s_" to help with clashes and to make it more obvious what the variable really is. With microcontrollers like the PIC which have limited variable space and especially limited stack space, it can be more efficient to use statics or grobals to more efficiently use memory. Also, from a speed standpoint, some microcontrollers can access static memory faster than a stack variable (I'm not sure about the PIC in this respect). Memory management with a microcontroller can take more understanding than for your typical PC program. |
|
#2
|
|||
|
|||
|
Re: Why Static Variables?
Excellent guys, thanks for the info.
|
|
#3
|
|||
|
|||
|
Re: Why Static Variables?
I actually wouldn't worry about static variables too much. They are an optimization that is rather easy to apply later.
Always optimize for readability. Always be prepared for the MacTruck Event. (explicitly, a person ((you)) gets removed from play, typically with out warning). I had to take over programming in my sophemore year of high school when our programmer suddenly could not attend competition. A few premature optimizations left the drivetrain code completely unreadable. I eventually had to kill that part and start over. Static variables are NO WHERE NEAR that level of confusing, but just keep in mind that other people will read the code. If an optimization took you a decent amount of time to figure out, it will take even longer for the reader to. Therefore, use local variables where it is good to do so. If you want to make it faster later, just type static in front of their declarations. I would hope the compilor would do that for you, but this ain't the brightest light in the box. |
|
#4
|
||||
|
||||
|
Re: Why Static Variables?
It seems to me like there are several conflicting explanations on the use of static variables.
There are two main types of static variables: 1. Static variables declared inside functions 2. And static variables declared outside functions The first type means that the variable is persistent between calls of the function. Let's say I have the function below: Code:
void function (void) {
static int counter = 0;
int fakeCounter;
counter++;
printf("Counter: %d\r\n", counter);
printf("Fake Counter: %d\r\n", fakeCounter);
}
----You can skip reading this if you wish, but it is interesting ----However, I believe you can use the "overlay" storage class in order to save CPU time since the variable is stored in normal RAM rather than the function stack. However, the variable is reinitialized upon every call of the function and other functions with overlay variables are allowed to use the memory locations of other overlay variables that are in functions which do not call the current function. See example below: Code:
void f1(void) {
overlay int i;
i = 0;
f2();
}
void f2(void) {
overlay int j;
j= 2;
}
void f3(void) {
overlay int k;
k = 3;
}
----Start reading again---- Now after this long digression, the explanation of what the other "static" means. When static is used with a global variable, it means that the global variable cannot be accessed from outside the file in which it is declared. That is, if i declare static int a = 0 in file1.c a function in file2.c cannot access the variable "a". This version of the static modifier can also be used with functions in order to indicate that the function cannot be called from outside the file. Quote:
I hope this insanely long and boring post was helpful ![]() |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
||||
|
||||
|
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:
|
|
#8
|
||||
|
||||
|
Re: Why Static Variables?
Quote:
On the other had if you have use of global variables then you have no conrtol over the data that the function is using. Vars could be changing mid-function making debugging work very hard. Example: Your using variable x which is global. You call function process_x() passing it no args since x is global and you dont need to. At some point within function process_x you call another function process_y. Since its been a while since you wrote process_y you forgot that it tweaks the value of x. Because of this process_x starts screwing up and you spend WAY to long debugging it. |
|
#9
|
||||
|
||||
|
Re: Why Static Variables?
Out of curiosity, is the only difference between a global variable accessed by the function and a static variable created and accessed by the same function scope? It seems that since both the static variable and the global variable are stored in standard RAM, they would have the same speed, and static variables could simply not be accessed by other functions as globals could. (This isn't to imply that statics are useless, as they would avoid many name conflicts, although I tend to name all of my variables exclusively anyway. I'm only trying to clarify that my grasp of their use is correct).
|
|
#10
|
|||
|
|||
|
Re: Why Static Variables?
Quote:
Quote:
Another example, although this one there's not a lot that can be done about it and it is relative rare but does occur, is variables associated with interrupts. They need to be clearly identified with the volatile keyword so everyone knows the value can change at any instant and if they're working directly on the variable, they need to protect it for the duration of use. Yet another example which doesn't apply to RC code is multithreaded code or shared variables in multiple processes. All of what applies to your example applies to these types of applications in spades. You don't get a nice linear debug path to find where the data is changing. |
![]() |
| 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 |