Question:

How many storage classes are available in C++?

Answer:

Storage class are used to specify the visibility/scope and life time of symbols(functions and variables). That means, storage classes specify where all a variable or function can be accessed and till what time those variables will be available during the execution of program.
Following storage classes are available in C++

  • auto

It's the default storage class for local variables. They can be accessed only from with in the declaration scope. auto variables are allocated at the beginning of enclosing block and deallocated at the end of enclosing block.

void changeValue(void)
{
   auto int i = 1 ;
   i++;
   printf ( "%d ", i ) ;
   
}
int main()
{
   changeValue();
   changeValue();
   changeValue();
   changeValue();
   return 0;
}
 
Output:-
2 2 2 2

In the above example, every time the method changeValue is invoked, memory is allocated for i and de allocated at the end of the method. So it's output will be same.

  • register

It's similar to auto variables. Difference is that register variables might be stored on the processor register instead of RAM, that means the maximum size of register variable should be the size of CPU register ( like 16bit, 32bit or 64bit). This is normally used for frequently accessed variables like counters, to improve performance. But note that, declaring a variable as register does not mean that they will be stored in the register. It depends on the hardware and implementation.

int main()
{
   register int i;
   int array[10] = {0,1,2,3,4,5,6,7,8,9};          
 
   for (i=0;i<10;i++)
   {
      printf("%d ", array[i]);
   }
   return 0;
}
 
Output:-
0 1 2 3 4 5 6 7 8 9

The variable i might be stored on the CPU register and due to which the access of i in the loop will be faster.

  • static

A static variable will be kept in existence till the end of the program unlike creating and destroying each time they move into and out of the scope. This helps to maintain their value even if control goes out of the scope. When static is used with global variables, they will have internal linkage, that means it cannot be accessed by other source files. When static is used in case of a class member, it will be shared by all the objects of a class instead of creating separate copies for each object.

void changeValue(void)
{
   static int i = 1 ;
   i++;
   printf ( "%d ", i ) ;
   
}
 
int main()
{
   changeValue();
   changeValue();
   changeValue();
   changeValue();
   return 0;
}
 
Output:-
2 3 4 5

Since static variable will be kept in existence till the end of program, variable i will retain it's value across the method invocations.

  • extern

extern is used to tell compiler that the symbol is defined in another translation unit (or in a way, source files) and not in the current one. Which means the symbol is linked externally. extern symbols have static storage duration, that is accessible through out the life of program. Since no storage is allocated for extern variable as part of declaration, they cannot be initialized while declaring.

int x = 10;
int main( )
{
   extern int y ;
   printf("x: %d ", x );
   printf("y: %d", y);
   return 0;
}
int y = 70 ;
 
Output:-
x: 10 y: 70

extern variable is like global variable, it's scope is through out the program. It can be defined anywhere in the c++ program.

  • mutable

mutable storage class can be used only on non static non const data a member of a class. Mutable data member of a class can be modified even is it's part of an object which is declared as const.

class Test
{
    public:
        Test(): x(1), y(1) {};
        mutable int x;
        int y;
};
 
int main()
{
    const Test object;
    object.x = 123;
    //object.y = 123;
    /* 
     * The above line if uncommented, will create compilation error.
     */	 
    cout<< "X:"<< object.x << ", Y:" << object.y;
    return 0;
}
 
Output:-
X:123, Y:1

In the above example, we are able to change the value of member variable x though it's part of an object which is declared as const. This is because the variable x is declared as mutable. But if you try to modify the value of member variable y, compiler will throw error.
 

You can find the summary of c++ storage class specifiers below

C++ Storage
Specifier
Storage
Location
Scope Of
Variable
Life Time
auto Memory (RAM) Local With in function
static Memory (RAM) Local Life time is from when the flow reaches the first declaration to the termination of program.
register CPU register Local With in function
extern Memory (RAM) Global Till the end of main program

Keywords:

© 2017 QuizBucket.org