on Leave a Comment

Storage Classes in C++

This article contains Storage Classes in C++.

As a variable has its data type, same a variable has its storage class. Storage class of a variable defines its scope and lifetime of a variable. If we do not mention the storage class of a variable, then C++ compiler automatically set the variable to its default storage class depending on the context of that variable.  

Value of a variable may stored in memory and CPU registers. Storage class of a variable defines where the value of variable will store.

Storage Class of a Variable defines the following things:

(1) Where the variable will store.
(2) Initial value of a variable.
(3) Life of a variable.
(4) Scope of a variable.

Storage Classes in C++

(1) Automatic
(2) Register
(3) Static
(4) External
(5) Mutable

Automatic Storage Class

auto keyword is used for automatic storage class. Default initial value of automatic variable is garbage value. Value of automatic storage class variable is stored in memory. Scope of automatic variable is local to the block in which it is defined. Lifetime of automatic variable is till the control remains in the block in which it is defined.

Program of Automatic Storage Class

#include<iostream>
using namespace std;
int main()
{
    auto int a=1;
    {
        auto int a=2;
        {
            auto int a=3;
            cout<<a;
        }
        cout<<endl<<a;
    }
    cout<<endl<<a;
    return 0;
}

OUTPUT:

3
2
1


Register Storage Class

register keyword is used for register storage class. Storage of register variable is in the CPU registers. Default initial value of register variable is garbage value. Scope of register variable is local to the block in which it is defined. Lifetime of register variable is till the control remains in the block in which it is defined.

Register variable is accessed faster because their value is stored in CPU register. Accessing time of a value stored in CPU registers is always less as compared to memory.

Program of Register Storage Class

#include<iostream>
using namespace std;
int main()
{
    register int i;
    for(i=1;i<=10;i++)
    {
        cout<<endl<<i;
    }
    return 0;
}

OUTPUT:

1
2
3
4
5
6
7
8
9
10

While declaring a variable to register storage class, it is not sure that variable will store in CPU register. Because the CPU has limited registers.


Static Storage Class

static keyword is used for static storage class. Storage of static variable is in memory. Default initial value of static variable is zero. Scope of static variable is local to the block in which it is defined. Value of static variable persists between different function calls.

Visibility of a static variable is in the function where it is defined. Life of static variable is till the program terminates. If we define a static variable inside a function, and when we call that function and change the value of static variable, and when the second time we call that function, the value static variable remains unchanged.

Program of Static Storage Class

#include<iostream>
using namespace std;
void fun(int a);
int main()
{
    fun(1);
    fun(2);
    return 0;
}
void fun(int a)
{
    static int s;
    s=s+a;
    cout<<endl<<s;
}

OUTPUT:

1
3


External Storage Class

External storage class variable stores inside the memory. Default initial value of external variable is zero. External variable is declared outside all function and it is accessible to all functions. Its scope is global. Lifetime of external variable is till the program terminates. 

Program of External Storage Class

#include<iostream>
using namespace std;
int a;
void fun(int i);
int main()
{
    fun(1);
    fun(2);
    return 0;
}
void fun(int i)
{
    a=a+i;
    cout<<endl<<a;
}

OUTPUT:

1
3


Mutable Storage Class

mutable keyword is used for mutable storage class. A class object or member function can be declared as const making their data not modifiable. But when we want to create const object and we want to modify a particular data only. In this situation we can make that data mutable.
Example: mutable int x;


0 comments:

Post a Comment