C++ - SPLessons

Storage Classes in C++

Home > Lesson > Chapter 12
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Storage Classes in C++

Storage Classes in C++

Storage Classes

shape Description

Storage Classes in CPP defines "Scope, Visibility and Life-time of a variable". It also decides where a variable or an object has to be stored and also declares the lifetime of these variables and objects.
  • The variables are allowed to retrieve only where their scope is declared.
  • To make a variable visible, a value must be assigned to it. The initialization of value becomes automatic if not given by the user.
Syntax:
storage_class declarator;

Types of Storage Classes in CPP

shape Description

The specifiers which determines storage classes are called as Storage Class Specifiers, which are categorized into 4 types.

Auto Storage

shape Description

Automatic Storage Class variables are defined within the function body. It is declared inside a function or block.
  • It is considered local to that program and they exist only when the block is in existence and cannot be accessed out of the block. Many variables can be named inside different functions, which are local to the functions where they are declared.
  • It uses the keyword auto.
Syntax:
auto datatype variable_name=value;
Eg: int x=10;  // considered as automatic by default If any value is not declared, the compiler takes the garbage value for automatic variables.

shape Example

[c] #include<iostream> using namespace std; void funct() { auto int x=2; cout<<x<<endl; } int main() { auto int x=1; cout<<x<<endl; funct(); }[/c] Output: [c] 1 2[/c]

Register Storage

shape Description

Register variables are same as Auto Storage Class variables but the variables are stored in registers to retrieve them faster to save the time. Once the use of function is completed, the values cannot be retrieved. Unlike C,  C++ allows to locate the address of register variables. It uses the keyword 'register'. Syntax:
register datatype variable_name=value;

shape Example

[c]register int x=10; int* y=&x;[/c]

Static Storage

shape Description

The objects or variables declared in local block are made static so that these objects exist even after the execution of block. The keyword used is static.
  • The static remains until the execution is completed and can be accessed whenever the function is called
  • Once declared, static variable or object copy, it can be shared by all the class instances
  • Static Variables acts as global variables but cannot be accessed in another file
  • "Type Declarations and Function Parameters" cannot use the properties of static storage class
Syntax:
static datatype variable name=value;

shape Example

[c] #include<iostream> using namespace std; void stat() { static int i = 1; cout<< i << endl; i++; cout << i <<endl; } int main() { static int x=2; cout << x << endl; x--; cout << x << endl; stat(); stat(); }[/c] Output: [c] 2 1 1 2 2 3[/c]

Extern Storage

shape Description

Extern Storage Classes in CPP helps to access the object or variable which is present in other file. Use extern keyword in front of the variable name which has to be initialized to access. So, it redirects the compiler to the file where the value is stored. Syntax:
extern datatype variable;

shape Examples

Consider two files "one.cpp and two.cpp". The below code is written in "one.cpp" [c] #include <iostream> int count ; extern void hello(); main() { count = 7; count++; hello(); }[/c] The below code is written in "two.cpp" [c] #include <iostream> using namespace std; extern int count; void hello(void) { cout << "Count is " << count << endl; }[/c] Output: [c] count is 8[/c] Now, "two.cpp" uses the value of c in "one.cpp" by using extern.

Mutable Storage

shape Description

Mutable is another storage class that is included in C++. It is used for class members only and hence when used this, the value declared as constant can be changed. For example, once if a value is declared as constant, it cannot be changed throughout the program. To change the constant value,mutable specifier is used. Syntax:
mutable data_type  variable_name;

shape Example

[c] #include<iostream> using namespace std; class spl { public :spl(int a, int b) { m = a; n = b; } int m; mutable int n; }; int main() { const spl les(1, 2); // m = 1 and n = 2 cout << "m : " << les.m << " n : " << les.n << endl; les.n = 3; // legal since 'n' is mutable cout << "m : " <<les.m << " n : " <<les.n << endl; return 0; }[/c] Output: [c] m:1 n:2 m:1 n:3[/c]

Summary

shape Key Points

  • Storage Classes in CPP decides "Scope, Visibility and Life-time of a variable"
  • Auto Storage class variables are deleted after execution
  • Register variables stores only some values
  • Extern variables are operated and declared outside the program
  • Static variables value can be changed as per the function call

shape Programming Tips

Getting the address of register variables is very difficult.