C++ Storage Class(Local, Global, Static Local, Register, Thread local)

C++ Storage Class(Local, Global, Static Local, Register, Thread local)

This article will teach you about C++ Storage Class. There are five types: local, global, static local, register, and thread-local.

In C++, every variable has two properties: type and storage class.

  • The type of data that can be stored in a variable is specified. For instance, int, float, char, and so on.
  • Storage Classes are used to define the characteristics of a variable or function. These characteristics include the scope, visibility, and lifetime, which allow us to track the presence of a variable during the course of a program’s execution. 
  • Furthermore, the storage class governs two different variables’ properties: lifetime (the length of time a variable can exist) and scope (determines which part of the programme can access it).

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

Local Variable In C++ Storage Class

A variable defined within a function (defined within braces within the function body) is known as a local variable or automatic variable.

Its scope is restricted to the function in which it is defined. To put it simply, a local variable exists and can be accessed only within a function.

  • A local variable is defined, initial values are set, and the variable is consumed within a function, method, or block.
  • These variables come to life only when the function that contains them is executed, and they are immediately destroyed when programme control is passed to the next function.
  • If a local variable is referenced outside of its function, method, or block, the programme throws an error.

When the function terminates, the life of a local variable expires (it is destroyed).

Example 1: Local variable In C++ Storage Class

#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;
}

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, since C++11, the auto has a new meaning and should not be used to define local variables.

Global Variable In C++ Storage Class

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 programme. This means that it can be used and updated at any point in the programme after being declared.

A global variable is one of the scope types in any programming language and is described in C++ as a variable that can be used or accessed from anywhere in the programme.

In general, a global variable is defined as a variable that is allowed to be used by any part of the programme without any restriction or error and is available to any part of the programme or throughout the entire programme.

However, they can be declared or defined at the top or start of the programme, which is outside of all the blocks and functions.

Similarly, its existence stops only when the programme does.

#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 preceding program.

This variable is visible to both the main() and test() procedures in the preceding programme.

Static Local variable in C++ Storage Class

A static variable is specified using the keyword static. As an example:

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

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

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

Example 3: Static local variable In C++ Storage Class

#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 programme.

Variable var is declared as a static variable and initialised to 0 after 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 method test() returns.

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

The output of the preceding programme if var was not defined as a static variable.

1
1

Register Variable In C++ Storage Class

The keyword register is used to specify variables in registers.

like automatic variables, register variables, exist solely within a single function. It should be faster than the local variables.

When a programme 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.

Thread Local Storage In C++ Storage Class

Thread-local storage is a way of allocating variables so that each thread has one instance of the variable.

For this purpose, the keyword thread local is utilised.

You may like:

C++ Function Overloading with Example

https://theudaipurstore.com/maharana-pratap/

Leave a Reply