STORAGE CLASSES
The period of time during which memory is associated with a variable is called the extent
of the variable. It is characterized by storage classes. The storage class of a variable indicates
the allocation of storage space to the variable by the compiler. Storage classes define the extent
of a variable. C++ supports the following four types of storage classes:
1. auto
2. register
3. extern
4. static.
The syntax for defining variables with explicit storage class is shown below. The storage
classes except extern are used for defining variables; extern is used for declaration of variables.
The scope and extent of auto and register storage class is the same. The scope of static variables
is limited to its block(maximum to a file), but its extent is throughout the execution time of the
program(does not matter whether it is local or global type).
Declaration Versus Definition:
A declaration informs the compiler about the existence of the data or a function
somewhere in the program. A definition allocates the storage location. In C++, a piece of data
or function can be declared in several different places, but there must only be one definition.
Otherwise, the linker will complain (generates multiple definition error) while uniting all the
object modules, if it encounters more than one definition for the same function or piece of data.
Almost all C and C++ programs require declarations. Therefore, it is essential for the
programmer to understand the correct way to write a declaration. As far as data is concerned,
except extern storage class, all others define data, i.e., they not only direct the compiler, but
also allocate resource for a variable.
Auto Variables
1. By default, all the variables are defined as auto variables. They are created when the
function/block is entered and destroyed when the function/block is terminated.
2. The memory space for local auto variables is allocated on the stack.
3. The global auto variables are visible to all the modules of a program, and hence, they
cannot be defined many times unlike the declarations.
Register Variables
1. The allocation of CPU (processor) registers to variables, speeds up the execution of a
program.
2. Memory is not referred when such variables are accessed.
3. The number of variables, which can be declared as register are limited (typically two or
three), within any function or as global variables (else they are treated as auto variables).
4. A program that uses register variables executes faster when compared to a similar program
without register variables.
5. It is possible to find out the allocation of register variables only by executing and
comparing the timing performance of the program (perceptible in large programs).
6. It is the responsibility of the compiler to allot register variables. In case the compiler is
unable to do so, these variables are treated as auto variables.
7. It is advisable to define frequently used variables, such as loop indices, as register
variables.
Static Variables
1. The static storage class allows to define a variable whose scope is restricted to either a
block, a function, or a file (but not all files in multimodule program) and extent is the
lifespan of a program.
2. The memory space for local static and global variables is allocated from the global heap.
3. Static variables that are defined within a function remember their values from the previous
call (i.e., the values to which they are initialized or changed before returning from the
function).
4. The static variables defined outside all functions in a file are called file static variables.
5. They are accessible only in the file in which they are defined.
The output of the program is a sequence of numbers starting with 1, rather than a string
of 1’s. The initialization of static variable count is performed only in the first instance of the
function call. In successive calls to the function, the variable count has the same value as it had
before the termination of the most recent call. However, these static variables are not accessible
from other parts of the program. Extern global variables are global to the file in which they are
defined. They are used when the same global variable is referenced in each one of the files and
these variables must be independent of each other across files. The use of global variables is
not recommended, since they do not allow to achieve function independence which is one of
the basic ideas of modular programming.
Extern Variables
When a program spans across different files, the files can share information using global
variables. Global variables must be defined only once in any of the program modules and they
can be accessed by all others. It is achieved by declaring such variables as extern variables. It
informs the compiler that such variables are defined in some other file. Consider a program
having the following files
In file1.cpp, the statement
int done;
defines the variable done as a global variable. In file2.cpp, the statement
extern int done;
declares the variable done and indicates that it is defined in some other file.
Note that the definition of the variable done must appear in any one of the modules,
whereas extern declaration can appear in any or all modules of a program. When the linker
encounters such variables, it binds all references to the same memory location. Thus, any
modification to the variable done is visible to all the modules accessing it. If the global variable
done is defined as static, it can be again defined in other modules since the linker treats each
as a different variable. Such global static variables have scope restricted to a file and extent is
equal to the entire lifespan of the program. The auto and static global variables are used mainly
in managing large multimodule software projects. Note that the memory space for a global
variable is allocated from the global heap memory.