The Working Programmer's Guide To Variables - Scope, Lifetime And More
Written by Harry Fairhead   
Article Index
The Working Programmer's Guide To Variables - Scope, Lifetime And More
Objects And Lifetimes
Existence

Existence

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.

This is of course the trick used in both C++, JavaScript and Object oriented Pascal to ensure that objects exist for the duration of the program - they are all declared at the highest level in the program!variablescope

The Stack 

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. 

Conclusion 

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. 

Related Articles

Binary - Negative Numbers

Binary Arithmetic

Inside the Computer - Addressing

The Heart Of A Compiler

The Fundamentals of Pointers

Computer Memory and Pigeonholes

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 
Banner


Kolmogorov Complexity

This xkcd cartoon provides an ideal excuse to explain Kolmogorov complexity. It is an interesting topic and one that gets right to the heart of programming of how programming relates to ideas like inf [ ... ]



Computer Memory and Pigeonholes

We discover why computer memory can be likened to pigeonholes and even include instructions for you to build your own memory device.


Other Articles
 

blog comments powered by Disqus

 

 

<ASIN:0201350882>

<ASIN:0201657880>

<ASIN:0321637135>

<ASIN:0521614104>



 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.