Storage Classes (C++)
Introduction
This blog will be covering the concept of Storage classes in the context of C++. Every variable in C/C++ has a storage class. Storage class determines the scope/lifetime, linkage and memory location of objects. A variable can have only one storage class. .Storage classes Following are the storage classes in C++.
auto
register
static
extern
mutable
thread_local
After reading this list you might have got a clear idea of what are the storage classes and what is going to be in the rest of the blog.
Auto
Auto is the default storage class of every variable. Variables defined within a block have automatic storage unless explicitly defined otherwise. Automatic objects and variables have no linkage, they are not visible to code outside the block.
{ int mount; auto int month; }
The example above defines two variables with the same storage class.
Register
In C++11, the register keyword is deprecated. It specifies that the variable is to be stored in a machine register, if possible. This means that the variable has a maximum size equal to the register size (32 or 64 bits). Like auto variables, register variables persist only until the end of the block in which they are declared.
{ register int reg_var; }
Declaring a variable as register does not mean that it will always be stored in a register. It means it MIGHT be stored in a register. If the address-of operator (&) is used on an object that is declared with register, the compiler must put the object in memory rather than a register.
Static
The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope.
#include <iostream> using namespace std; void showstat( int curr ) { static int nStatic; // Value of nStatic is retained // between each function call nStatic += curr; cout << "nStatic is " << nStatic << endl; } int main() { for ( int i = 0; i < 5; i++ ) showstat( i ); }
output
nStatic is 0 nStatic is 1 nStatic is 3 nStatic is 6 nStatic is 10
This example shows how a variable declared static in a function retains its state between calls to that function.
If we make a const variable static it causes that variable's scope to be restricted to the file in which it is declared.
Extern
Objects and variables declared as extern declare an object that is defined in another scope or transition unit as having external linkage. For example we declare a function header with extern keyword in one file (.cpp) and provide its implementation in another file(.cpp).
//File1.cpp
#include <iostream> int count ; extern void write_extern();
main() { count = 5; write_extern(); }
#include <iostream> extern int count; void write_extern(void) { std::cout << "Count is " << count << std::endl; }
Output
Count is 5
Mutable
Mutable storage class allows a member of an object to override constness. That is, a mutable member can be modified by a const member function.
class X { public: bool GetFlag() const { m_accessCount++; return m_flag; } private: bool m_flag; mutable int m_accessCount; };
Thread_local
This is available in C++11. A variable declared with the thread_local specifier is accessible only on the thread on which it is created. The variable is created when the thread is created, and destroyed when the thread is destroyed. Each thread has its own copy of the variable.
The thread_local specifier may be combined with static or extern. You can apply thread_local only to data declarations and definitions.











