Broad Network


Automatic and Dynamic Storage Duration in C++

Storage Duration in C++ - Part 1

Forward: In this part of the series I explain Automatic and Dynamic Storage Duration in C++.

By: Chrysanthus Date Published: 25 Aug 2012

Introduction

This is part 1 of my series, Storage Duration in C++. In this part of the series I explain Automatic and Dynamic Storage Duration in C++.

Life of an Object
In C++, an object is a region in memory that can have a value. Each such region has an identifier. If the identifier is erased, the object ceases to exist, because the region would no longer be identifiable. When an object ceases to exist, you can say the object is dead. So an object is born when it is created and it dies when it ceases to exist. Some objects die at the end of the program execution. Many die before the execution of the program ends. The lifetime of an object is the storage duration of the object.

Scope and Storage Duration
One of my previous series is titled, Scopes in C++. In that series I explain the meaning of scope in C++. You should read that series first before starting this one. With the exception of static objects, extern objects (see later) and the namespace scope, an object can only be accessed within its scope. C++ storage duration of an object, is another way of explaining scoping in C++. Storage duration also gives you an in-depth look in scoping.

Types of Storage Duration
There are three types of storage duration, which are:

- Automatic storage duration
- Dynamic storage duration
- Static storage duration

In this part of the series, I explain automatic storage duration and dynamic storage duration. In the next and last part of the series, I explain static storage duration.

Automatic Storage Duration
Automatic storage duration applies to a typical local scope object. This is like the default storage duration. Consider the following program:

#include <iostream>
using namespace std;

void fn()
    {
        //some statements
        int myInt = 3;
        cout << myInt;
    }


int main()
    {
        fn();


        return 0;
    }

In the function scope (block) of the fn() function, the object myInt is created. It is created in the statement, “int myInt = 3;”. It dies at the end of the scope (block). That is, it dies when the function called, reaches, } , at the end of its block. The object identified by, myInt, is of automatic storage duration.

Any object of automatic storage duration, dies at the end of its scope. Remember, a local scope may have a nested local scope. If an object of automatic storage duration is declared in the outer local scope, it will be seen in the inner local scope, but will die at the end of its outer local scope.

The specifier for automatic storage duration is, “auto” or “register”. You can precede an object identifier of automatic storage duration with either of these words. If you do not precede the identifier with any of these words, the object is still of automatic storage duration. In other words, the specifier for automatic storage duration is optional (to write).

In the above code it was not typed (written). In the following code that works, it is typed:

#include <iostream>
using namespace std;

void fn()
    {
        //some statements
        auto int myInt = 3;
        cout << myInt;
    }


int main()
    {
        fn();


        return 0;
    }

Note where the specifier, auto, has been typed in the declaration (creation) of the myInt object.

Dynamic Storage Duration
Objects of dynamic storage duration are objects created in free store. Free store is computer memory that is not part of the memory given to your program. Objects of this type are created using the globally accessible predefined, new and new[] operators. An object created with the, new operator can only be destroyed (killed) with the, delete operator. An object created with the, new[] operator can only be destroyed with the, delete[] operator. The delete and delete[] operators are globally accessible predefined operators. These globally accessible predefined operators are from the standard library (see later).

In the following code, a dynamic storage object is created and destroyed:

#include <iostream>
using namespace std;

void fn()
    {
        float *myFltPtr = new float;
        *myFltPtr = 2.5;

        cout << *myFltPtr << "\n";

        delete myFltPtr;
    }

int main()
    {
        fn();

        return 0;
    }

Consider the following code where the dynamic object is not destroyed with the delete operator.

#include <iostream>
using namespace std;

void fn()
    {
        float *myFltPtr = new float;
        *myFltPtr = 2.5;

        cout << *myFltPtr << "\n";
    }

int main()
    {
        fn();

        return 0;
    }

For this last code, by the time the execution of the code (program) reaches its end, the dynamic object is not destroyed, because of the absence of the delete operator. Normally all objects are destroyed by the end of the program execution, except dynamic objects.

A dynamic object is created in a scope and destroyed in that scope. You cannot create a dynamic object in one scope and then destroy it in another scope, everything being equal.

You, the coder, is the one who creates a dynamic object in a scope and destroys it in the scope. If you do not destroy a dynamic object in its scope, the object will not be destroyed through out the program. You can only destroy it in its scope, not out of the scope, everything being equal.

So, the life of a dynamic object begins when it is created. It dies when it is destroyed using the delete (or delete[]) operator. Everything being equal, you create a dynamic object in a scope and destroy it in the scope. If you do not destroy it in the scope, it would still be alive, as the program continues to execute out of the scope, but you will not be able to destroy it out of scope, everything being equal.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

C++ Taking the Bull by the Horns
C++ Just After the Basics
Object Oriented Programming in C++
C++ Operators
C++ Templates
Exception Handling in C++
Container Library Sequences in C++ Simplified
Associative Container in C++ Simplified
String in C++ Standard Library Simplified
Date and Time in C++ Simplified
Advanced Course
Storage Duration in C++
Scopes in C++
Function and Operator Overloading in C++
Specifiers in C++
Some Features of C++ Entities
C++ Preprocessing Directives
Writing a C++ Container
C++ Namespace
Writing a C++ Application
More Related Links
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
C++ Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message