Tutorial Study Image

Storage Class in C++


September 1, 2022, Learn eTutorial
1076

This Tutorial will teach you about various storage classes in C++. There are mainly five types: local, global, static local, register, and thread-local.

In C++, every variable has two properties: type as well as a storage class. The type of data that can be stored in a variable is specified. For example, int, float, char, and so on. Besides that, the storage class controls two different variables' properties such as lifetime (the length of time a variable can exist) and scope (which determines which part of the program can access it).

Within a C++ program, the Storage class is used to define the lifetime and visibility of a variable and/or function. The variable's lifetime is the amount of time it is active, and its visibility is the module of a program in which it is accessible.

A variable can be classified into four major types based on its storage class:

  1. Local variable
  2. Global variable
  3. Static local variable
  4. Register Variable
  5. Thread Local Storage

1.    Local variable

A local variable, also known as an automatic variable, is a variable that is defined inside a function (it will be defined inside the function body between braces). Its scope is restricted to the function in which it is defined. This means a local variable exists and can be accessed only within a function. When the function exits, the life of a local variable ends (it is destroyed).

Example for Local Variable


#include <iostream>

using namespace std;

void test();

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

    test();

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

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

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

Var cannot be used within test(), and var1 cannot be used within the main() function.

Previously, the keyword auto was used to define local variables as auto int var;

However, after C++11, the auto has a new meaning and should not be used to define local variables.

2.    Global Variable

When a variable is defined outside of all functions, it is referred to as a global variable. A global variable's scope is the entire program. This means that it can be used and changed at any point in the program after being declared.

Similarly, its existence ends only when the program terminates or ends.

Example for 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

C is a global variable in the program mentioned above. This variable is visible to both the main() and test() functions in the preceding program.

3.    Static local variable

A static variable is specified using the keyword static. For example:


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

A static local variable exists only within the function where it is declared (like a local variable), but its lifetime begins when the function is called and ends only when the program terminates.

The primary distinction between a local variable and a static variable is that the value of a static variable is retained at the end of the program.

Example for 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

The test() function is called twice in the preceding program.

Variable var is declared as a static variable and initialized to 0 during the first call. The number one is then added to var, which is displayed on the screen.
Because variable var is a static variable, it remains after the function test() returns.

There is no new variable var created during the second function call. The same variable is increased by one and then displayed on the screen.

If var was not specified as a static variable, the output of the previous program will be :
1
1

4.    Register variable 

Register variables are specified using the keyword register. Register variables, like automatic variables, exist only within a single function. It needs to be faster than the local variables.

When a program encounters a register variable, it stores it in the processor's register rather than memory, if memory is available. As a result, it is faster than the local variables. This keyword, however, was deprecated in C++11 and should not be used.

5.    Thread local storage

Thread-local storage is a kind of mechanism for allocating variables so that each thread has one instance of the variable.For this purpose, the keyword thread_ local is used.

Types of Storage class in C++

There are five different kinds of storage classes that can be used in a C++ program.

  1. Automatic
  2. Register
  3. Static
  4. External
  5. Mutable
C++ Storage Class

1. Automatic Storage Class

It is the storage class for all local variables by default. The auto keyword is automatically applied to all local variables.


{   
   auto int y;  
   float y = 3.45;  
} 
 

In the above example, two variables with the same storage class are defined; the auto may only be used within functions.

2.    Register Storage Class

The register variable stores memory in a register rather than RAM. Its size is the same as the register size. It can be accessed more quickly than other variables.

It is recommended that you only use register variables for quick access, like in a counter.
 

Note : We cannot obtain the address of the register variable.


register int counter=0;    
 

3. Static Storage Class

The static variable is only initialized once and exists until the program terminates. It keeps its value even when multiple functions are called. The compiler provides the default value 0 for the static variable.

or an example


#include <iostream>  
using namespace std;  
void func() {    
    static int i=0; //static variable    
    int j=0; //local variable    
    i++;    
    j++;    
    cout<<"i=" << i<<" and j=" <<j<<endl;    
}    
int main()  
{  
    func();    
    func();    
    func();    
} 

Output:

i= 1 and j= 1
i= 2 and j= 1
i= 3 and j= 1