Free Hosting

File scope and the static keyword


In previous lessons, you learned about local variables (which have block scope) and global variables (which have program scope). There is one other level of scoping that variables can have: file scope.
A variable with file scope can be accessed by any function or block within a single file. To declare a file scoped variable, simply declare a variable outside of a block (same as a global variable) but use the static keyword:
1
2
3
4
5
6
7
static int nValue; // file scoped variable
float fValue; // global variable
 
int main()
{
    double dValue; // local variable
}
File scoped variables act exactly like global variables, except their use is restricted to the file in which they are declared (which means you can not extern them to other files). File scoped variables are not seen very often in C++ because they have most of the downsides of global variables, just on a smaller scale.
The static keyword
The static keyword is probably the most confusing keyword in the C++ language. This is because it has different meanings depending on where it is used. When applied to a variable declared outside of a block, it changes the variable from a global variable to a file scoped variable. When applied to a variable declared inside a block, it has a different meaning entirely!
By default, local variables have automatic duration, which means they are destroyed when the block they are declared in goes out of scope. You can explicitly declare a variable as having automatic duration by using the auto keyword, though this is practically never done because local variables are automatic by default, and it would be redundant.
Using the static keyword on local variables changes them from automatic duration to fixed duration (also called static duration). Afixed duration variable is one that retains it’s value even after the scope in which it has been created has been exited! Fixed duration variables are only created (and initialized) once, and then they are persisted throughout the life of the program.
The easiest way to show the difference between automatic and fixed duration variables is by example.
Automatic duration (default):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
 
void IncrementAndPrint()
{
    using namespace std;
    int nValue = 1; // automatic duration by default
    ++nValue;
    cout << nValue << endl;
} // nValue is destroyed here
 
int main()
{
    IncrementAndPrint();
    IncrementAndPrint();
    IncrementAndPrint();
}
Each time IncrementAndPrint is called, a variable named nValue is created and assigned the value of 1. IncrementAndPrint increments nValue to 2, and then prints the value of 2. When IncrementAndPrint is finished running, the variable goes out of scope and is destroyed. Consequently, this program outputs:
2
2
2
Now consider the fixed scope version of this program. The only difference between this and the above program is that we’ve changed the local variable nValue from automatic to fixed duration by using the static keyword.
Fixed duration (using static keyword):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
 
void IncrementAndPrint()
{
    using namespace std;
    static int s_nValue = 1; // fixed duration
    ++s_nValue;
    cout << s_nValue << endl;
} // s_nValue is not destroyed here, but becomes inaccessible
 
int main()
{
    IncrementAndPrint();
    IncrementAndPrint();
    IncrementAndPrint();
}
In this program, because s_nValue has been declared as static, s_nValue is only created and initialized (to 1) once. When it goes out of scope, it is not destroyed. Each time the function IncrementAndPrint() is called, the value of s_nValue is whatever we left it at previously. Consequently, this program outputs:
2
3
4
Using hungarian notation, it is common to prefix fixed duration variables with “s_”. Some programmers use “s” (which we don’t like as much because that letter is better used for structs) or “c_” (which we don’t like as much because it’s not as mnemonic).
One of the most common uses for fixed duration local variables is for unique identifier generators. When dealing with a large number of similar objects within a program, it is often useful to assign each one a unique ID number so they can be identified. This is very easy to do with a fixed duration local variable:
1
2
3
4
5
int GenerateID()
{
    static int nNextID = 0;
    return nNextID++;
}
The first time this function is called, it returns 0. The second time, it returns 1. Each time it is called, it returns a number one higher than the previous time it was called. You can assign these numbers as unique IDs for your objects. Because nNextID is a local variable, it can not be “tampered with” by other functions.

1 comment:

  1. A Static Variable is visible or accessible only within the class or only by members of class But its lifetime is throughout the program means it can hold the same value throughout the lifetime of program

    There is good reference for Need of Static Data Member and Data Functions here :
    http://geeksprogrammings.blogspot.com/2014/02/static-keyword.html

    ReplyDelete

Blogger Template by Clairvo