C++ Storage classes

Storage class defines the visibility and life of variables or functions within a C++ Program.These specifiers precede the type that they modify. There are many Storage classes, which are defined below.

  • Auto Storage class
  • Register Storage class
  • Static Storage class
  • Thread_local Storage class
  • Extern Storage class
  • Mutable Storage class

  Auto storage:-

auto storage class is the default storage class for all local variables.

{

Int a;

Auto int b;

}

Auto is used for local variables.

Register Storage Class:-

In register storage class, variables are stored in registers of cpu. scope of variables are local to the functions in which variables are defined. control of variables remains within the block in which the variable is defined.

Static :-

Static variable is local to the function in which it is declared,
but the value of variable persist between different functions calls. Default initial value of variable is 0.
When we declare a variable, we use static keyword in front of variable. Memory is allocated when the program start and memory released when the program execution end.

void test{
 static int a=1;
 cout<<a<<endl;
 a++;
 }

Thread_local Storage class:-

In thread_local storage class we allocate one variable with one thread. thread_local storage class use global variables. we use __thread specifier, with the extern or static specifiers. __thread specifier may be used  with static file, static function, static member of a class.

Extern Storage class :-

Extern storage class have global scope and the life of the variable as long as execution of program’s execution doesn’t come to an end. we use extern keyword in front of variable. Variable of extern keyword always global and any part of program can use them(These variables are accessible by any function).
#include<conio.h>
#include<iostream.h>
extern int a;
void main()
{

}

Mutable Storage class :-

When you want to modify data members but don’t want to update other members of a class, then we use the mutable storage class.But data members can not be static or const. Visibility have local and initial value
garbage. mutable keyword is used.




Follow Us

Contact Us

tutorialworldin@yahoo.com

Back to Top