Static Storage Class (C++)

Storage Classes in C++
In C++, storage classes defines the lifetime, and linkage of objects and variables. A given object can have only one storage class. Five types of storage classes in c++ are

  1. Automatic
  2. Static
  3. Register
  4. External
  5. Mutable

Automatic storage class:
The auto storage-class specifier can be used only to declare variables used in blocks or to declare formal arguments to functions. The term “auto” comes from the fact that storage for these objects is automatically allocated at run time (normally on the program’s stack). Objects and variables defined within a block have auto storage unless otherwise specified using the extern or static keyword. These objects or variables persist only until the end of the block in which they are declared.

class A
{
};
void Function()
{
auto A objA1; // function scope
// variable explicitly declared as auto
// inner block
{
A objA2; // inner block scope
// implicitly an auto variable
}
}

Static Storage class:
Static storage means the objects or variables declared with static duration has the program scope. That means the object or variable is allocated when the program starts and is de-allocated only when the program ends. Objects or variables declared outside all blocks (i.e. global objects or variables) has static scope and external linkage.

Usage of static key word under various scenarios

  1. During declaration of variable or function at file scope (i.e. global scope), the static keyword specifies that the variable or function has internal linkage. Compiler automatically initializes the static variable to zero (i.e. 0) unless another value is not specified.
  2. A variable declared using static keyword in a function specifies that the variable retains its value between calls to that function.
  3. void ShowValueOfStaticVariable( int curr )
    {
    static int nStaticVar = 0; // this statement execute only once
    // so initialization happens only once
    nStaticVar += curr;
    cout << “nStaticVar is ” << nStaticVar << endl;
    }

    int main()
    {
    for ( int nItr = 0; nItr < 5; nItr++ )
    {
    ShowValueOfStaticVariable( nItr );
    }
    }

    Output:

    nStaticVar is 0
    nStaticVar is 1
    nStaticVar is 3
    nStaticVar is 6
    nStaticVar is 10

  4. Static data member declared inside a class ensures that all instances of that class shares single instance of that data member inside a process. The static data member should be defined at file scope.
  5. class A
    {
    public:
    static int m_nStaticVar; // static variable declaration
    static void ShowValueOfStaticVariable()
    {
    cout << “m_nStaticVar is ” << nStaticVar << endl;
    };
    };

    int A::m_nStaticVar = 0; // static variable definition
    A objA1;
    A objA2;

    int main()
    {
    objA1.m_nStaticVar = 10;
    objA1.ShowValueOfStaticVariable();
    objA2.ShowValueOfStaticVariable();

    objA2.m_nStaticVar = 20;
    objA1.ShowValueOfStaticVariable();
    objA2.ShowValueOfStaticVariable();
    }

    Output:

    m_nStaticVar is 10
    m_nStaticVar is 10
    m_nStaticVar is 20
    m_nStaticVar is 20

  6. Static member functions are accessible at the class scope, with out creating instance of that class and it is shared by all instances of that class. A static function does not have an implicit this pointer and so it cannot access an instance member.
    1. In the above sample code, the static member variable “m_nStaticVar” of class A is accessible at the class scope as “A::m_nStaticVar”
    2. We cannot access the static variable from inside any of its member functions as “m_nStaticVar” which is implicitly calling “this->m_nStaticVar”.

    Note:- Assigning a value to a static local variable in a multi-threaded application is not thread safe and it is not recommend as a programming practice.

    Register Storage class:
    The register keyword specifies that the variable is to be stored in a machine register, if possible. By declaring a variable with register storage class hints the compiler that it will be accessed very frequently during execution.

    register int nRVar;

    Compiler will not use the registers for storing these variables under the following conditions

    1. If number of register variables are large
    2. register int R1,R2,R3,R4,R5, … Rn; // can also produce an error
      // results in adding a register to stack
      // as the CPU runs out of physical registers
    3. If tried to access the address of register variables
    register int R1;
    int* pnVal = &R1; // address of register variable

    Extern Storage class:
    The extern keyword declares a variable or function and specifies that it has external linkage (its name is visible from files other than the one in which it is defined). Declarations of variables and functions at file scope are external by default.

    MainFile.cpp

    extern int nVariable;

    int main()
    {
    cout << “Extern Variable Value = ” << nVariable;
    }
    |SubFile.cpp
    |
    | int nVariable = 10;
    |
    |
    |
    |
    |
    |
    | ______________

    Output:

    Extern Variable Value = 10

    Extern key word with a string is also used to specify the linking conventions of another language. C functions and data defined in another seperately compiled translation unit can be accessed only if they are declared as having C linkage. The extern “C” syntax is used to link C funtions defined in another run time library.

    extern “C” int Function1();
    extern “C” int Function2();

    or

    extern “C” {
    int Function1();
    int Function2();
    }

    Mutable Storage Class
    Mutable keyword can be applied only to a member variable. When a member variable is declared as mutable, then it can be modified from a const member function

    class A
    {
    public:
    bool GetResult() const
    {
    m_nValue++;
    returnm_bResult;
    };
    mutable int m_nValue;
    bool m_bResult;
    };

    Static functions

    1. Static functions declared inside a class can be considered as global functions declared inside some namespaces. Instead of namespaces, the function can be accessed using the class scope.
    class A
    {
    public:
    static void ShowValueOfStaticVariable();
    };

    int main()
    {
    A::ShowValueOfStaticVariable()
    }
    Advertisements
This entry was posted in Technical and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s