C++ Storage Class explanation with simple examples

Storage class defined for a variable defines the accessibility and longevity of the variable. The accessibility of the variable relates to the portion of the program that has access to the variable. The longevity of the variable refers to the length of time or time span the variable exists within the program.In other words, A storage class defines the scope (visibility) and life time of variables and/or functions within a C++ Program.

Program variables have a storage class in addition to a data type. Storage classes are important in C++ for several reasons. One, they tell the compiler how to create and release variables and where to place them in the run-time environment (stack, data area, or CPU registers). Two, storage class specifiers affect the initial values and the scope of variables.

Types of Storage Class Variables in C++:

  • auto (Automatic) storage class
  • register storage class
  • static storage class
  • extern (External) storage class

Automatic:

Variables defined within the function body are called automatic variables. Auto is the keyword used to declare automatic variables. By default and without the use of a keyword, the variables defined inside a function are automatic variables.

 The term automatic variable is used to define the process of memory being allocated and automatically destroyed when a function is called and returned. The scope of the automatic variables is only within the function block within which it is defined. Automatic variable are also called local variables.

auto int var1; // declared with auto specifier
int var1; //declared without the storage class specifier

Both the above declarations will produce the same result.

 Register

The register storage class is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and can’t have the unary ‘&’ operator applied to it (as it does not have a memory location).

The register should only be used for variables that require quick access such as counters. It should also be noted that defining ‘register’ goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register depending on hardware and implementation restrictions but it is not always mandatory.

 External:

External variables are also called global variables. External variables are defined outside any function, memory is set aside once it has been declared and remains until the end of the program. These variables are accessible by any function. This is mainly utilized when a programmer wants to make use of a variable and access the variable among different function calls.

Static:

The static automatic variables, as with local variables, are accessible only within the function in which it is defined. Static automatic variables exist until the program ends in the same manner as external variables. In order to maintain value between function calls, the static variable takes its presence.

In C++, when static is used on a class data member, it causes only one copy of that member to be shared by all objects of its class.

 void static_function_csetips()
{
static int x = 0;
x++;
cout << x <<endl;
}
If this function is called 10 times, the output will be 1,2,3,4..etc., The value of the variable x is preserved through function calls.

Mohit Arora
Follow me