In this article, we’ll learn about storage classes present/used in C++. So, first of all we should know what is a storage class and what does it do?
Storage Class:
It is basically a class of storage which the compiler needs when storing a variable and/or function. Storage class defines the scope or what we call as visibility and lifetime of the variable. Lifetime of a variable means the time period till which that variable will be active in a program and scope means till which point the variable can be used or called or will be accessible.
We know in C there are 4 types of storage classes:
- Automatic
- Register
- External
- Static
In addition to all these, in C++ we have one more storage class:
5. Mutable
Let’s discuss more about these five storage classes.
Automatic Storage Class:
Variables specified with this storage class are local variables. In other words, they have local scope. So, its lifetime is within the function and it can be used only inside that function not outside the function. The keyword used for automatic storage is auto. If we don’t specify any storage class during declaration of the variable, the compiler treats the variable as automatic storage class implicitly by default. Hence, it is also called default storage class.
Example Code:
#include <iostream> using namespace std; void autoStorageClass() { cout<<"Examples of AutoStorageclass"<<endl; auto int a = 16; auto float b = 2.7; auto char c = 'S'; cout << a << " \n"; cout << b << " \n"; cout << c << " \n"; } int main() { autoStorageClass(); return 0; }
Output:
Register Storage Class:
So, for otherstorage classes the variable is stored in RAM but for Register storage class the variable will be assigned to register memory. But there is a disadvantage which is that the CPU will be having limited register memory. So, it depends on the availability of the register memory. If the register memory is available then the data will be stored otherwise it will be lost. The size of the variable depends on the size of the register. The keyword used for Register storage class is register.
Example Of Register Storage Class:
#include <iostream> using namespace std; void registerStorageClass() { cout << "Register class Example\n"; register int i = 234; cout << "Value of the variable i:"<< i; } int main() { registerStorageClass(); return 0; }
Output:
External Storage Class:
These variables which are declared with Extern keyword can be used outside the program also. The same variable could be used for two or more files through external storage class.
Example Of External Storage:
//Example code: subfile.cpp int sum=20; void add(int s) { sum=sum+s; }
Output Of External Storage:
Example Of External Storage:
Mainfile.cpp: #include<iostream> #include "subfile.cpp" using namespace std; extern int sum; int main() { cout<<sum<<endl; add(20); cout<<sum<<endl; return 0; }
Output Of External Storage:
Static Storage Class:
In static storage class, the variable has a lifetime of the entire program. The variable will not be destroyed even if the pointer has come out of the function where the variable is declared. The variable is only initialized once. So, the keyword used for this storage class is static.
Example Static Storage Class:
#include <iostream> using namespace std; int staticvariables() { cout << "For static variables: "; static int i = 5; i++; return i; } int main() { cout<<"Displaying static variables incremented."<<endl; cout << staticvariables() << "\n"; cout << staticvariables() << "\n"; return 0; }
Output Of Static Storage Class:
Mutable Storage Class:
This storage class is only used/applied to class objects. By using the Mutable storage class, we can override a constant member function with a member of an object which means the constant member function can modify a mutable member.
Example Of Mutable Storage Class:
#include <iostream> using std::cout; class mute { public:int a; mutable int b; mute() { a = 6; b = 24; } }; int main() { const mute m1; m1.b = 50; cout << m1.b; return 0; }
Output Mutable Storage Class:
As you can see in the above example the output is 50 which means variable b is modified.
So, we learnt about every storage class in C++ but while using them we have to remember some data given in the table below.
Storage class | Keyword | Initial Value | Lifetime | Visibility |
Automatic | auto | garbage | Function block | Local |
Register | register | garbage | Function block | Local |
Static | static | zero | Whole program | Local |
External | extern | Zero | Whole program | Global |
Mutable | mutable | garbage | Class | Local |
Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about this topic discussed above.