STORAGE CLASS in C++

STORAGE CLASS IN C++
STORAGE CLASS IN C++

C++ provides five storage-class specifiers: auto,register,extern,mutable and static.An identifier’s storage-class specifier helps determine its storage class and linkage. Whereas storage-class specifier mutable is used exclusively with C++ user-defined types called classes. The storage-class specifiers can be split into two storage classes: automatic storage class and static storage class. Keywords auto and register are used to declare variables of the automatic storage class. Such variables are created when the program execution enters the block in which they are defined, they exist while the block is active and they are destroyed when the program exits the block

Only local variables of a function can be of the automatic storage class. A function’s local variables and parameters normally are of the automatic storage class. The storage class specifier auto explicitly declares the variables of the automatic storage class.

When an object is declared, not only are the object’s type and name defined but also its storage class.

Page Contents:

Auto Class :
Extern Class :
Static Class :
Register Class :
What are the identifier’s scope and linkage?

Auto class :

Object with block scope is normally created automatically within the code block that defines them. Such an object can only be accessed by a statement within that block and is called local to that block. the memory used for these objects is freed after leaving the code block. In this case, the lifetime of the objects is said to be automatic.

For example:
{
 auto int y;
 float y = 3.45;
 } 

Extern class :

If an object is not defined within a function it belongs to the extern storage class. Objects in this storage class have program scope and can be read and, provided they have not been defined as const, modified at any place in the program.

For example:
extern int counter=0; 

Static class :

If an object definition is preceded by the static keyword, the object belongs to the static storage class.

Example for Static class:

#include <iostream> 
using namespace std; 
void func() { 
static int i=0; //static variable 
int j=0; //local variable 
i++; 
j++; 
cout<<"i=" << i<<" and j=" <<j<<endl; 
} 
int main() 
{ 
func(); 
func(); 
func(); 
} 

OUTPUT:

i= 1 and j= 1
i= 2 and j= 1
i= 3 and j= 1

Register class :

To increase the speed of a program, a commonly used auto variable can be stored in the CPU register instead of on the stack. In this case, the register keyword is used to declare the object. A register is normally the size of an int variable.

register int counter=0; 

What are the identifier’s scope and linkage?

1.Identifier scope: An identifier’s scope is where the identifier can be referenced in a program. Some identifier can be referenced throughout a [program; others can be referenced from only limited portions of a program

2.identifier linkage: An identifier’s linkage determines for a multiple-source file program whether an identifier is known only in the current source file or in any source file with the proper declaration

Note* The storage-class specifier register can be placed before an automatic variable declaration to suggest that the compiler maintain the variable in one of the computer’s high-speed hardware registers rather than in memory. If intensely used variables such as counters or totals can be maintained in hardware registers, the overhead of repeatedly loading the variables from memory into the registers and storing the results back into memory can be eleiminated

Translate »