In this article,we are going to learn about storage class.
Every variable in C++ has two features: type and storage class.
Type specifies that what kind of data can be stored in a variable. For
example: int
, float
, char
etc.
And, storage class determine two things of the variable i.e. lifetime (how long the variable be in the memory) and scope (from where, till where we can access the variable).
Depending upon the storage class of a variable, it can be divided into 4 major types :
A local variable is defined inside a function (i.e. inside the braces of the function definition) and it can only be accessed within that function this is called local or automatic variable.
Its scope is limited to that function only (i.e. it can’t be accessed from outside the function body in which it is declared).
The life of the variable gets finished as soon as that function is ended in other words we can say that it gets erased from the memory as soon as the function end.
When a variable is declared outside all the functions it is said to be a global variable.
Its scope is valid in whole program that is it can be accessed from anywhere and by anyone in the whole program.
Like it's scope, its lifetime is also valid throughout the program that is it will have a storage in memory from the start of the program till the termination of the program.
Example 1:
#include <iostream> using namespace std; // Global variable declaration int count = 12; void example(); void example2(); int main() { ++count; // Outputs 13 cout << count << endl; example(); example2(); return 0; } void example() { int i = 9; ++count; ++i; // Outputs 10 cout << i; // Outputs 14 cout << count; } void example2() { ++count; ++i; // invalid code cout << i; // Outputs 15 cout << count; }
In the above program, count
is a global variable and i
is the local variable
for the function example()
. So, the variable count is available for each and
every function in the program but variable i
is only available for the function
example()
and it cannot be used outside anywhere except inside the
braces of function example()
. So it will generate error when we will use it
inside example2()
or anywhere except example()
.The error that compiler
will generate undefined symbol 'i when we use it inside example2()
.
Keyword static is used for specifying a static variable. For example :
... .. ... int main() { static float a; ... .. ... }
A static local variable scope is limited to that function in which it is defined same as that of local variable but its lifetime is throughout the program that is its last value resides in the memory after the function’s execution is terminated.
For more clarity let’s take an example :
#include <iostream> using namespace std; void example() { // var is a static variable static int count = 2; ++count; cout << count << endl; } int main() { example(); example(); return 0; }
Output :
3 4
In the above program, example()
function is invoked 2 times.
During the first call, variable count
is declared as static variable and
initialized to 0. Then 1 is added to count
which is
displayed in the screen.
When the function example()
returns, the variable var
should be
2 but since
the variable var
is a static variable it's last value was still inside the memory.So,
during second function call, no new variable var
is created. The same var
is
increased by 1 and then displayed to the screen.
Keyword register
is used for specifying register variables
Register variables are similar to automatic variables and exists inside a particular function only. It is supposed to be faster than the local variables.
If a program encounters a register variable, it stores the variable in processor's register rather than memory if available. This makes it faster than the local variable.
Thread-local storage is a mechanism by which variables are allocated such that there is one instance of the variable per extant thread.
Keyword thread_local
is used for this purpose.