C++ Storage Classes
This article covers C++ Storage Classes with working examples.It also includes Automatic Storage Class,register storage class,static storage class,extern storage class and mutable storage class.
Introduction
A storage class describes a variable or function’s characteristics. These characteristics, which primarily include scope, visibility, and lifetime, allow us to track a particular variable’s availability throughout a program. In this article, we will talk about storage classes in detail, along with the features of storage classes. We will also discuss various types of storage classes along with their codes in C++ language.
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
Table of contents
- Storage Class in C++
- Automatic Storage Class
- Register Storage Class
- Static Storage Class
- Extern Storage Class
- Mutable Storage Class
- Conclusion
Storage Class in C++
Storage class in C is used for specifying a variable or function’s lifetime and visibility. Visibility is the section of a program in which a variable is accessible, whereas the lifetime is the length of time the variable is active.
In a C++ program, five different kinds of storage classes can be utilized. Let us see and learn about them in the section below:
- Auto
- Register
- Extern
- Static
- Mutable
The syntax for the storage class is given below for specifying the storage class for a variable:
storage_class data_type_var name_var;
Let us see the table below to understand more about the storage classes and their features:
Storage Class name | Keyword Used | Visibility | Lifetime | Initial Value |
Automatic | Auto | Local | Function block | Garbage |
Register | register | Local | Function block | Garbage |
External | extern | Global | Whole program | Zero |
Static | static | Local | Whole program | Zero |
Mutable | mutable | Local | Class | Garbage |
Explore popular Programming Courses
Automatic Storage Class
The auto keyword has type inference capabilities, allowing for the automatic determination of an expression’s data type in a programming language. Having to put out information that the compiler knows already saves time. Since all types are determined during the compilation process alone, this tiny increase in compilation time has no impact on the program’s runtime.
Additionally, methods and non-type template arguments are covered by this functionality. Since C++14, the return type for functions will be inferred from their return statements. Since C++17, the type will be inferred from the argument for template parameters that don’t have types.
Let us see a C++ code below and understand the working on auto storage class in a better manner:
#include <iostream>using namespace std;void autoStorage(){ cout << "The Demonstration of auto class\n"; auto a = 12; auto b = 1.2; auto c = "Shiksha Online"; auto d = 'N';
cout << a << " \n"; cout << b << " \n"; cout << c << " \n"; cout << d << " \n";}int main(){ autoStorage();
return 0;}
The output of the code will be:
The Demonstration of auto class
12
1.2
Shiksha Online
N
In the above example, we have initialized four auto variables, a,b,c, and d, where there is no need for any data-type declaration. The auto variables are then printed using c << out, and the auto class is demonstrated using autoStorage() method.
Register Storage Class
This storage class declares register variables with the same capability as auto variables. The compiler only tries to save these values in the microprocessor register if an empty register is available, which is the only distinction—because of this, using the register variables throughout program execution is much faster than using memory-stored variables. These are only kept in the memory of an empty register is unavailable. The register keyword is typically used to define a few variables in a program that will be accessed frequently, which reduces the amount of time the program must run.
The fact that we cannot use pointers to get the address for a register variable is both significant and intriguing in this context.
Let us see a C++ code below and understand the working on register storage class in a better manner:
#include <iostream>using namespace std;void registerStorage(){ cout << "The Demonstration of register class\n"; register char x = 'G'; cout << "The value of the variable 'x'" << " declared as register: " << x;}int main(){
registerStorage(); return 0;}
The output of the code will be:
The Demonstration of register class
The value of the variable ‘x’ declared as register: G
In the above example, we have initialized a register variable as char x with a value ‘G. The register variable x is then printed using c << out, and the register class is demonstrated using the registerStorage() method.
Static Storage Class
Static variables frequently utilized while building C++ applications can be declared using this storage type. Static variables can retain their value after being used outside of their intended context! Static variables, therefore, maintain the result of the most recent use within their scope.
Hence, we can conclude by saying that they are initialized just once and remain in existence until the program is finished. Since they aren’t re-declared, no additional memory is allocated. The function that they specified is the only area of their application. Anywhere in the program can access global static variables. The compiler gives them the value zero by default.
Let us see a C++ code below and understand the working on static storage class in a better manner:
#include <iostream>using namespace std;int staticClass(){ cout << "Static variables: "; static int count = 1; count++; return count;}int nonstaticClass(){ cout << "Non-Static variables: "; int count = 0; count++; return count;}int main(){ cout << staticClass() << "\n"; cout << staticClass() << "\n"; ; cout << nonstaticClass() << "\n"; ; cout << nonstaticClass() << "\n"; ; return 0;}
The output of the code will be:
Static variables: 2
Static variables: 3
Non-Static variables: 1
Non-Static variables: 1
In the above example, we have initialized a function named staticClass() containing the static variables where memory is retained during the execution time. Then we have defined another function named nonstaticClass() for non-static variables, and here, the memory is not retained at the time of execution. Hence memory is destroyed. Both the static and non-static variables are called using c << out; hence we get the output.
Extern Storage Class
The term “external storage class” indicates that the variable’s definition is located outside the block in which it is utilized. In essence, the value is set to it in one block and can be altered or rewritten in another. Therefore, a global variable initialized with a valid value where it is defined to be applied somewhere else is all that an extern variable is. It is accessible from any function or block. Furthermore, by adding the ‘extern’ keyword before a standard global variable’s declaration or declaration in any function or block, a variable can also be turned extern. This implies that we are not initializing a new variable; instead, we are accessing the global variable also.
Extern variables are primarily used so that they can be accessed from two separate files that are a component of a vast program.
Let us see a C++ code below and understand the working on the extern storage class in a better manner:
#include <iostream>using namespace std;int a;void externStorage(){ cout << "The Demonstration of extern class\n";
extern int a; cout << "The value of variable 'a' " << "declared, as extern: " << a << "\n"; a = 4;
cout << "Modified value of the variable 'a'" << " declared as extern: \n" << a;}int main(){ externStorage(); return 0;}
The output of the code will be:
The Demonstration of extern class
The value of variable ‘a’ declared, as extern: 0
Modified value of the variable ‘a’ declared as extern:
4
In the above example, we have declared a variable ‘a’, which will be made extern. By the statement, extern int a, we are trying to imply to the compiler that the variable a is defined somewhere else, above the primary function. The extern value will be printed using c << out, and later the value of extern variable a is modified to a=4; hence we get the output.
Mutable Storage Class
Even when you wouldn’t want the function to modify other components of the class or struct, there are times when you must edit more than one data member through a constant function. Using the keyword ‘mutable’ will make this task simple. The word “mutable” is mostly used to enable the modification of a specific data member of a constant object. This pointer supplied to the function becomes const when we define a function as constant. A constant pointer can alter members by inserting mutable to a variable.
Let us see a C++ code below and understand the working on mutable storage class in a better manner:
#include <iostream>using std::cout;class MutTest {public: int a; mutable int b; MutTest() { a = 2; b = 30; }};int main(){ const MutTest t1; t1.b = 60; cout << t1.b; return 0;}
The output of the code will be:
60
In the above example, we have declared a mutable variable ‘b’ as mutable int b, which can later be modified. Then in the main class, we define another variable, t1, which is set to a constant which will modify the value. The modified value will be printed using cout << t1.b, and later the value of the mutable variable as 60.
Conclusion
In this article, we explored the various storage classes that we can use in C++ programs, what they are, and how to utilize them.
The various ways that we can use them as storage classes in C++ programs, in addition to what they are and their functions, were covered in this article.
After doing so, the learner should be able to develop programs that employ storage classes to convey the characteristics of variables and procedures. I hope this information was clear to you and was helpful for your upcoming activities.
Contributed by Megha Chadha
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio