Broad Network


Static Storage Duration in C++

Storage Duration in C++ - Part 2

Forward: In this part of the series, I explain Static Storage Duration in C++.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 2 of my series, Storage Duration in C++. I assume, you have read the previous tutorial before reaching here. This is a continuation. In this part of the series, I explain Static Storage Duration in C++. This is to do with static objects in C++, that is, object declared with the specifier, static.

Meaning of Static Object
If you have been reading the tutorials in this volume, in the order given, as you should, you should have realized that I have written two tutorials on static objects. The two tutorials are titled, “Static Objects in C++” and “Static Members and the this keyword in C++ Classes”.

An object of automatic storage duration (local scope) dies at the end of its scope as the program execution continues. In those two tutorials I explained that, if you do not want that to happen, you precede the object declaration with the specifier, static. In that way, the object lives from its point of declaration in the local scope to the end of the program. And that is how I explained static objects. In this tutorial, I explain static objects from a different angle.

Objects of Global Scope
Consider the following program:

#include <iostream>
using namespace std;

int theInt1 = 10;

void fn()
    {
        int theInt2 = 20;
        cout << theInt2 << '\n';
        cout << theInt1;
    }

int main()
    {
        fn();

        return 0;
    }

In the program, the object, theInt1, is of global scope and the object, theInt2, is of function (or block scope). The object of global scope is seen in the “local” scope of the function. However, the function scope object cannot be seen outside the function (local) scope.

The life of an object of global scope, begins from the point of creation and dies at the end of the program execution. That is, it dies at the end of the file, as the program executes. After the point of creation, the global scope object can be seen (used) anywhere in the program (in blocks and nested blocks – that is, in scopes and nested scopes).

A global scope object, is an object that is not declared inside any scope (block).

Static Object
An object of local scope (automatic storage duration) dies at the end of its scope as the program executes. If you want, such an object to behave as a global object, you have to precede it with the specifier, static, in the declaration. When you do this, you convert the object from that of automatic storage duration to static storage duration. That is, the object will be alive after the execution of its local scope is complete. The static object will be alive from its point of creation till the end of the program (file) execution.

Accessing a Static Object outside Local Scope
Everything being equal, you cannot access an ordinary static object outside its local scope, but it is alive outside its local scope, downward.

The following code illustrates how the static object, n, is alive after the function (local) scope execution ends.

#include <iostream>
using namespace std;

void fn()
    {
        static int n = 0;

        cout << n << '\n';

        n = n + 2;

    }


int main()
    {
        fn();
        fn();
        fn();
        fn();

        return 0;
    }

The output of the above program is:

0
2
4
6

The function, fn() is called 4 times in main. If the static object, n, in this program, had to die at the end of the function (local) scope of fn(), then the output would be the same in each line, that is:

0
0
0
0

Read the above program again to see how this all works out; and be sure that a static object does not die after the execution of its scope. Do not confuse between execution of a scope and execution of the whole program.

C++ OOP
You cannot access a static ordinary object outside its local scope, but it is alive outside its local scope, downward, everything being equal. However, in C++ OOP, you can access a static data member (property) outside and below its local scope using the scope resolution operator, ::. The following program illustrates this:

#include <iostream>
using namespace std;

class MyClass
    {
        public:
        static int sameAll;
    };

int MyClass::sameAll = 5;

int main()
    {

        MyClass myObj;
        cout << myObj.sameAll;
        
        return 0;
    }

Note how the scope resolution operator has been used to access the static data member (object), sameAll, to give it the value of 5. In C++ OOP, after creating the object, you can still access it with the dot operator, using the instantiated object name; this is what happens in main, above. To use the scope resolution operator, you have to use (precede it with) the class name and not the instantiated name.

Note: A static object, not declared in a class, has to be initialized at declaration, as with,

        static int n = 0;

in the previous program. Replacing this statement with,

        static int n;
        n = 0;

does not work.

Note: C++ OOP does not allow initialization of a class data member (property), in the class description. In C++ OOP, indirect initialization of data members can only be done during instantiation, with the class constructor. A static data member cannot also be initialized in the class description, everything being equal. So, replacing the declaration of the above static data member with,

        static int sameAll = 5;

does not work. However, with the static data member, you can initialize it outside the class description either with the class name and scope resolution operator or indirectly during instantiation, with the constructor.

Before we continue, let me say again, the meaning of a static object. A static object, is an object declared (defined) in local (block) scope, but instead of dying at the end of the local scope, it lives on like a global object. To achieve this, you precede the normal automatics storage object declaration with, static, instead of the optional, auto. Unlike, the global object that can be accessed anywhere in the program, the static object (data member) can be accessed only when dealing with classes (OOP), but it is alive, outside its declaration scope.

The lifespan of a static object is the static storage duration (from creation to end of program).

Static Nature of Namespace Objects
As you must have noted in some of the previous tutorials in this volume, a namespace can take a definition, declaration, initialization, function definition, class descriptions (definitions) and other things.

You can access the namespace objects anywhere (global and local scopes) in the program below the namespace scope, using the namespace name and the scope resolution operator. You can also access a namespace object inside the namespace but without the scope resolution operator, below the point of declaration. The following program illustrate these:

#include <iostream>
using namespace std;

namespace myName
    {
        int ident1 = 33;
        int ident2 = 40;
        int herInt = ident1;
    }

int hisInt = myName::ident1;

int main()
    {
        cout << hisInt << '\n';
        cout << myName::ident2 << '\n';
        cout << myName::herInt;

        return 0;
    }

If you do not want to use the scope resolution operator, then you can use the “using namespace directive” as in the following program:

#include <iostream>
using namespace std;

namespace myName
    {
        int ident1 = 33;
        int ident2 = 40;
        int herInt = ident1;
    }

using namespace myName;

int hisInt = ident1;

int main()
    {
        cout << hisInt << '\n';
        cout << ident2 << '\n';
        cout << herInt;

        return 0;
    }

Whether you are applying the scope resolution operator or not, the namespace object is accessed outside the namespace scope, meaning the object is alive outside the namespace. So you can consider namespace objects as static object. You cannot consider them as global objects, because they are declared inside a scope (block).

That is it for this part of the series. We have come to the end of the series.

I assume you have completed this series. It means you already had the professional skills in C++. Now you are in an advanced course of C++. You may already be working. If you ever have too much work, you can subcontract (outsource) some or all to someone of the site below.

Chrys

Programming Solutions

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem

Comments

Become the Writer's Fan
Send the Writer a Message