C++ Storage Class

Hi everyone, inside this article we will see about C++ Storage Class.

In C++, storage classes define the scope (visibility) and lifetime of variables and functions. They determine how variables are stored in memory and how long they remain accessible.

The following are the four main storage classes in C++:

  • Auto
  • Register
  • Static
  • Extern

Here is a comparison of the four C++ storage classes:

Storage ClassScopeLifetimeInitializationMemory Location
autoLocalAutomatic destruction at end of blockAutomaticStack
registerLocalAutomatic destruction at end of blockAutomaticRegister (if available) or Stack
staticLocalPersistentAutomatic, but only initialized onceData segment
externGlobalPersistentNone (must be defined elsewhere)Data segment

Scope‘ refers to the visibility of the variable, and determines where the variable can be accessed from.

Lifetime‘ refers to how long the variable remains in memory and accessible.

Initialization‘ refers to whether the variable is automatically initialized when it is declared or if it must be explicitly initialized elsewhere.

Memory Location‘ refers to where the variable is stored in memory, and can affect the speed of access and the size of the program.

C++ Auto Storage Class

The “auto” storage class in C++ is the default storage class for local variables, declared within a function. An automatic storage class variable is created on the stack and is automatically destroyed when the block in which it is declared ends.

Syntax

auto x = value;

Here’s an example:

#include <iostream>
using namespace std;

void print_value(int value)
{
    auto x = value;
    cout << "The value of x is: " << x << endl;
}

int main()
{
    print_value(10);
    return 0;
}

Output

The value of x is: 10

In this example, the variable x is declared as an auto storage class variable within the print_value function. The variable x is created on the stack when the function is called, and is destroyed automatically when the function ends. The value of x is set to the argument passed to the function (value), and is then used to print a message to the console.

C++ Register Storage Class

The “register” storage class in C++ is used to declare variables that should be stored in a register, instead of in memory. This allows for faster access to the variable, as the register is a high-speed memory area located on the computer’s CPU.

However, the number of registers is limited, so it’s not always possible to store a variable in a register. In such cases, the compiler will store the variable in memory instead.

Syntax

register int x = 10;

Here’s an example:

#include <iostream>
using namespace std;

int main()
{
    register int x = 10;
    cout << "The value of x is: " << x << endl;
    return 0;
}

Output

The value of x is: 10

In this example, the variable x is declared as a register storage class variable. This means that the compiler will try to store the variable in a register, if possible. The value of x is set to 10, and is then used to print a message to the console.

C++ Static Storage Class

The “static” storage class in C++ is used to declare variables that have a local scope, but retain their value between function calls.

A static storage class variable is only initialized once, when the program starts. The variable remains in memory for the entire lifetime of the program, and retains its value between function calls.

Syntax

static int x = 0;

Here’s an example:

#include <iostream>
using namespace std;

void increment_value()
{
    static int x = 0;
    x++;
    cout << "The value of x is: " << x << endl;
}

int main()
{
    increment_value();
    increment_value();
    increment_value();
    return 0;
}

Output

The value of x is: 1
The value of x is: 2
The value of x is: 3

In this example, the increment_value function declares a static storage class variable x. The first time the function is called, the variable x is initialized to 0. The value of x is then incremented each time the function is called, and is used to print a message to the console.

Because x is a static storage class variable, its value is retained between function calls, so each time the function is called, the value of x is one greater than it was the last time the function was called. This allows us to keep track of the number of times the function has been called, even though the variable x has a local scope.

C++ External Storage Class

The “extern” storage class in C++ is used to declare variables that have a global scope, but are defined in another source file. The extern storage class is used to provide a reference to a global variable that is defined in another source file.

Syntax

extern int x;

Here’s an example:

File main.cpp:

#include <iostream>
using namespace std;

extern int x;

int main()
{
    cout << "The value of x is: " << x << endl;
    return 0;
}

File x.cpp:

int x = 10;

In this example, the variable x is defined in the x.cpp file as a global variable, with a value of 10. The main.cpp file uses the extern storage class to declare a reference to the variable x, which is defined in the x.cpp file. The value of x is then used to print a message to the console.

It’s important to note that the use of the extern storage class does not actually define the variable, it only provides a reference to a variable that has already been defined. The variable must be defined in exactly one source file, and can be referenced from any number of other source files using the extern storage class.

We hope this article helped you to understand about C++ Storage Class in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.