Page 3 of 3
Existence is an aspect of variables that most programmers rarely consider because it is an obvious consequence of the type of language that they most use.
For example, in Fortran and Basic variables are usually static, in block structured languages such as Java, C and C# they are usually dynamic or automatic as the term "dynamic" is used for so many things.
- A static variable is one that exists for the duration of the program.
- An automatic variable is created each time a function is run and then destroyed when it ends.
In languages such as Fortran and Basic static variables are the norm.
In block structured languages automatic variables are the norm.
Static variables can be used to store state information such as the number of times a function has been called. As a static variable retains its value between calls you can simply add one each time the function is called. Automatic variables make it impossible to set up a use counter in this way because they are destroyed when the function terminates. In this case you have no choice but to use a global variable or at least one that lives long enough to retain the count.
Automatic variables minimise storage requirements by assuming that if a procedure isn't running then its local variables cannot be in use by any other procedure.
Of course in a block structured language automatic variables exist when the block they are declared in is executing and they are destroyed when the block is completed.
This existence rule is just an extension of the nested scope rule - if a variable is in scope then it exists if it isn't then it doesn't.
Even block structured languages normally allow the programmer to create static variables on demand. Usually by declaring them using a a STATIC modifier. Languages that don't have a STATIC modifier and so don't appear to recognise the existence of static variables allow you to create static variables as global variables. A variable that has global scope will exist for the whole program and so can be described as a global static variable.
You might be wondering what the advantage of static variables is that many early languages adopted them as the only type they provided?
Well the answer is that a static variable can be assigned to an area of storage by the compiler at compile time and it stays there at the same location throughout the run of the program. This makes static variables simpler and faster than automatic variables.
Automatic variables on the other hand cannot be allocated at compile time they have to allocated to storage at run time. Fortunately this isn't as difficult as it sounds because most block structured languages use some sort of execution stack.
When a new procedure is started the old variables are pushed on the stack and the new procedures are allocated. When the procedure is finished the original procedure's variables are pulled off the stack. Using the stack makes automatic variables very easy to implement.
At this point you can now see that variables can be classified in two ways Scope - local, nested, global; and Existence - static and automatic.
No doubt there are other subtle ways of managing variables their scope, visibility, lifetime and so on - but this more or less corresponds to what most programmers have to deal with today.
in general when learning a new language you have to ask what the rules are for the binding of a name to an entity i.e. its scope and you have to ask about the question of the lifetime of that binding. There are many variations of possible answers.
Binary - Negative Numbers
Inside the Computer - Addressing
The Heart Of A Compiler
The Fundamentals of Pointers
Computer Memory and Pigeonholes