Variables and Fundamental Types

Storage Duration and Variable Lifetime

All variables are stored in memory for a finite amount of time.

That time span is known as its lifetime.

The conventions defining the commencement and the expiration of the lifetime are called storage duration.

So knowing this, we will be able to determine when a variable is being created, destroyed, and when its value can be used.

Most variables that are declared within a function, if not explicitly specified, have automatic storage duration.

They are instantiated when the program traverses their declaration and destroyed automatically when the block in which they were declared terminates.

#include <iostream>

int main()
{
    std::cout << "Entering main\n";

    {
        int number{42};

        std::cout << number << '\n';
    }

    // std::cout << number << '\n';

    std::cout << "Leaving main\n";
}

This variable is created as soon as its declaration is run.

The name of the variable goes away, as well as its value, as soon as you hit the end of the block it has been declared in, at its closing brace.

The most typical pattern in C++ programs is therefore automatic creation and destruction.

When blocks are nested, the rule for each variable is still the same: it survives only so long as its own block.

#include <iostream>

int main()
{
    int outer{10};

    {
        int inner{20};

        std::cout << outer << ' '
                  // Use the newline escape sequence inside this literal
                  << inner << '\n';
    }

    std::cout << outer << '\n';
}

The outer variable is still present when the inner block runs.

The inner variable is created and destroyed entirely inside the nested block.

Their lives are independent, even though their timelines overlap for a brief moment.

Any variable declared using the static keyword inside a function has static storage duration.

It is created only one time, the first time we reach its declaration, and it exists until the end of the program.

#include <iostream>

void counter()
{
    static int count{0};

    // Increment the value before the expression uses it
    ++count;

    std::cout << "Count is "
              << count
              << '\n';
}

int main()
{
    counter();
    counter();
    counter();
}

Despite the fact that the variable has been declared in the scope of the function, it is not removed once the function returns.

The next call has the same variable with the same value as it had at the end of the previous call.

I use static local variables sparingly.

While they may be handy for trivial counting or one time setup, they conceal a persistent state which can make things more difficult to follow in some programs.

Scope specifies where a name may appear in source code.

Lifetime determines when the object exists.

A variable can be out of scope but still alive, such as a static local, or be in scope only during a short block with a likewise brief lifetime, as in the usual automatic case.

0 comments

No comments yet. Be the first.

Add a comment

About CppLearn

CppLearn is an interactive, free reference for modern C++. In each lesson, you'll find out how the language works, rather than just lines of code that you can copy and paste. We cover concepts starting from beginner lessons on fundamentals and proceed to templates, move semantics, smart pointers, and areas that usually trap or scare C++ learners.