I l@ve RuBoard |
![]() ![]() |
9.1 Scope and Storage ClassAll variables have two attributes, scope and storage class. The scope of a variable is the area of the program where the variable is valid. A global variable is valid from the point it is declared to the end of the program. A local variable's scope is limited to the block where it is declared and cannot be accessed (set or read) outside that block. A block is a section of code enclosed in curly braces ({ }). Figure 9-1 illustrates the difference between local and global variables. Figure 9-1. Local and global variables![]() It is possible to declare a local variable with the same name as a global variable. Normally, the scope of the variable count (first declaration in Figure 9-2) would be the whole program. The declaration of a second, local count takes precedence over the global declaration inside the small block where the local count is declared. In this block, the global count is said to be hidden. You can also nest local declarations and hide local variables. These "very local" variables have an even smaller and more local scope than the "normal local" variables. (The clarity of the previous sentence gives you some idea why using nesting to hide local variables does not make your program easy to understand.) Figure 9-2 illustrates a hidden variable. Figure 9-2. Hidden variables![]() The variable count is declared both as a local variable and as a global variable. Normally the scope of count (global) would be the entire program, but when a variable is declared inside a block, that instance of the variable becomes the active one for the length of the block. The global count has been hidden by the local count for the scope of this block. The shaded area in the figure shows where the scope of count (global) is hidden. It is not good programming practice to hide variables. The problem is that when you have the statement: count = 1; it is difficult to tell which count you are referring to. Is it the global count—the one declared at the top of main—or the one in the middle of the while loop? It is better to give these variables different names, such as total_count, current_count, and item_count. The storage class of a variable may be either permanent or temporary. Global variables are always permanent. They are created and initialized before the program starts and remain until it terminates. Temporary variables are allocated from a section of memory called the stack at the beginning of the block. If you try to allocate too many temporary variables, you will get a stack overflow error. The space used by the temporary variables is returned to the stack at the end of the block. Each time the block is entered, the temporary variables are initialized. The size of the stack depends on the system and compiler you are using. On many Unix systems, the program is automatically allocated the largest possible stack. On other systems, a default stack size is allocated that can be changed by a compiler switch. Local variables are temporary unless they are declared static.
Example 9-1 illustrates the difference between permanent and temporary variables. We have chosen obvious variable names; temporary is a temporary variable and permanent is permanent. C++ initializes temporary each time it is created (at the beginning of the for statement block); permanent gets initialized only once, at program start-up time. In the loop both variables are incremented. However, at the top of the loop -- temporary is initialized to 1. Example 9-1. perm/perm.cpp#include <iostream> int main( ) { int counter; // loop counter for (counter = 0; counter < 3; ++counter) { int temporary = 1; static int permanent = 1; std::cout << "Temporary " << temporary << " Permanent " << permanent << '\n'; ++temporary; ++permanent; } return (0); } The output of this program looks like: Temporary 1 Permanent 1 Temporary 1 Permanent 2 Temporary 1 Permanent 3 Table 9-1 describes the different ways a variable can be declared.
9.1.1 The for ScopeThe for statement is similar to a set of curly braces in that you can declare variables inside the statement whose scope goes from the start of the for to the end of the statement. (This includes the statement or block controlled by the for.) In the following statement: for (int count = 0; count < MAX; ++count) sum += count; // count is out of scope from here on. the variable count is declared inside the for. Its scope is to the end of the statement; the scope ends with the first semicolon after the for. |
I l@ve RuBoard |
![]() ![]() |