x

C++ Storage Class Specifiers

PREV     NEXT

 What are storage specifiers?

  • The storage specifiers define the scope or visibility and the lifetime of the variable or function within a program.
  • The lifetime is a duration till which the variable will remain active and visibility is in which the module of the program the variable is accessible.
  • The storage specifiers is like how storage is allocated and how variable is treated by compiler depends on the storage classes.

The storage specifiers are divided into 5 parts:

  • auto
  • static
  • register
  • extern
  • mutable

Let us discuss about these specifiers in brief:

Auto

The auto storage specifier is the default storage specifier for all local variables. It is applied to local variables and is visible only inside the function where it is declared and it dies as soon as function execution is over.


Let us have a look at the syntax:

Let us have a look at the example:

Static

  • In this the compiler receives the instructions from the static storage specifier to store the local variable during the lifetime of the program instead of creating or destroying it again and again as it comes into or goes out of the scope.
  • So, making local variables static allows them to maintain their values in between function calls. The static variables are initialized and allocated storage only once at the beginning of program execution and it can be called any number of times and used within the program.

Let us have a look at the example:

  • In the above program, the ‘a’ is static so it will retain the value throughout the function call and is initialized only once at the starting.

Register

  • The register storage specifiers are used to define the local variables that are to be stored in a register instead of RAM that means that variable has a maximum size equal to the register size.
  • In other words, it assigns a variable storage in the CPU register rather than a primary memory. It has the lifetime and visibility same as of automatic variable.
  • It is basically used to tell the compiler to make access to the variable as fast as possible. It increases the access speed.

Let us have a look at the example:

Extern

  • The extern storage specifier is used to access the variable from a file which is declared and defined in other file. It is used to give the reference of the global variable that is visible to all the program files.
  • As you use the extern keywords the variable cannot be initialized as it point the variable name at a storage location that is defined previously. When you have multiple files and you define a global variable or function which is used in other files also, then the extern keyword will be used in another file to provide reference of defined variable or function.
  • They are visible throughout the program and their lifetime remains till the program is there in which it is declared.

Let us have a look at the example:

A.cpp

Main.cpp

  • In the above example you have two files one is A.cpp and Main.cpp in which a variable temp is declared as external in Main.cpp. As it is a global variable and it is assigned value 10 in A.cpp.
  • So, it can be accessed in both the files. The sum ( ) adds the values of temp with the parameter which is passed while invoking it.
  • Now, the program will perform the multiplication and changes the global variable temp to 15.

Mutable

  • The mutable storage specifier is applied only to class objects. In this, it allows a member of an object to override the const member function which means a mutable member can be modified by a const member function.
  • The best example to understand Mutable Storage specifier is Bank example. In the bank transfer, the transaction needs to be locked so that no information can be changed but still the state has to be changed from started to complete and in between the status is in processing.
  • So, in that case you can make the variable modifiable using a mutable storage class.

Let us have a look at the example:

PREV     NEXT



Like it? Please Spread the word!