C++ Storage Class

In this article, you’ll find out about various storage classes in C++. Specifically: local, global, static local, register, and thread-local.

Each variable in C++ has two highlights: type and storage class.

Type determines the sort of information that can be stored in a variable. For instance: int, float, char, and so on.

And, storage class controls two unique properties of a variable: lifetime (decides how long a variable can exist) and scope (figures out which some portion of the program can get to it).

Local Variable

A variable characterized inside a function (characterized inside the function body between braces) is known as a local variable or automatic variable.

Its extension is just restricted to the capacity where it is characterized. In basic terms, the local variable exists and can be accessed just inside a function.

The life of a local variable closes (It is pulverized) when the function exits.


Example 1: Local variable

#include <iostream>
using namespace std;

void test();

int main() 
{
    // local variable to main()
    int var = 5;

    test();
    
    // illegal: var1 not declared inside main()
    var1 = 9;
}

void test()
{
    // local variable to test()
    int var1;
    var1 = 6;

    // illegal: var not declared inside test()
    cout << var;
}

The variable var cannot be used inside test() and var1 cannot be used inside main() function.

Keyword auto was also used for defining local variables before as: auto int var;

But, after C++11 auto has a different meaning and should not be used for defining local variables.


Global Variable

If a variable is characterized outside all capacities, at that point it is known as a global variable.

The extent of a global variable is the entire program. This implies It can be utilized and changed at any piece of the program after its affirmation.

Moreover, its life closes just when the program closes.


Example 2: Global variable

#include <iostream>
using namespace std;

// Global variable declaration
int c = 12;

void test();

int main()
{
    ++c;

    // Outputs 13
    cout << c <<endl;
    test();

    return 0;
}

void test()
{
    ++c;

    // Outputs 14
    cout << c;
}

Output

13
14

In the above program, c is a global variable.

This variable is visible to both functions main() and test() in the above program.


Static Local variable

Keyword static is used for specifying a static variable. For example

... .. ...
int main()
{
   static float a;
   ... .. ...
}

A static local variable exists just inside a function where it is declared (like a local variable) however its lifetime begins when the capacity is called and closes just when the program closes.

The main distinction between nearby factors and the static variables is that the value of the static variable endures the finish of the program.


Example 3: Static local variable

#include <iostream>
using namespace std;

void test()
{
    // var is a static variable
    static int var = 0;
    ++var;

    cout << var << endl;
}

int main()
{
    
    test();
    test();

    return 0;
}

Output

1
2

In the above program, test() function is invoked 2 times.

During the first call, variable var is declared as static variable and initialized to 0. Then 1 is added to var which is displayed in the screen.

When the function test() returns, variable var still exists because it is a static variable.

During the second function call, no new variable var is created. The same var is increased by 1 and then displayed to the screen.

Output of above program if var was not specified as static variable

1
1

Register Variable (Deprecated in C++11)

Watchword register is used for indicating register variables.

Register variables are like automatic variables and exist inside a specific capacity as it were. It should be quicker than the nearby factors.

If a program experiences a register variable, it stores the variable in the processor’s register instead of memory if accessible. This makes it quicker than the local variables.

In any case, this catchphrase was deplored in C++11 and ought not be used.


Thread Local Storage

Thread local storage is a component 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.

Learn more about thread local storage.


Please feel free to give your comment if you face any difficulty here.