Search

4.3 — Static duration variables

The static keyword is one of the most confusing keywords in the C++ language (maybe with the exception of the keyword “class”). This is because it has different meanings depending on where it is used.

In section 4.2 -- Global variables, you learned that when applied to a variable declared outside of a block, it defines a global variable with internal linkage, meaning the variable could only be used in the file in which it was defined.

The static keyword can also be applied to variables declared inside a block, where it has a different meaning entirely. In section 4.1a -- Local variables and local scope, you learned that local variables have automatic duration, which means they are created when the block is entered, and destroyed when the block is exited.

Using the static keyword on local variables changes them from automatic duration to static duration (also called fixed duration). A static duration variable (also called a “static variable”) is one that retains its value even after the scope in which it has been created has been exited! Static duration variables are only created (and initialized) once, and then they are persisted throughout the life of the program.

The easiest way to show the difference between automatic and static duration variables is by example.

Automatic duration (default):

Each time incrementAndPrint is called, a variable named value is created and assigned the value of 1. incrementAndPrint increments value to 2, and then prints the value of 2. When incrementAndPrint is finished running, the variable goes out of scope and is destroyed. Consequently, this program outputs:

2
2
2

Now consider the static scope version of this program. The only difference between this and the above program is that we’ve changed the local variable value from automatic to static duration by using the static keyword.

Static duration (using static keyword):

In this program, because s_value has been declared as static, s_value is only created and initialized (to 1) once. When it goes out of scope, it is not destroyed. Each time the function incrementAndPrint() is called, the value of s_value is whatever we left it at previously. Consequently, this program outputs:

2
3
4

Just like we use “g_” to prefix global variables, it’s common to use “s_” to prefix static (static duration) variables. Note that internal linkage global variables (also declared using the static keyword) get a “g_”, not a “s_”.

One of the most common uses for static duration local variables is for unique identifier generators. When dealing with a large number of similar objects within a program, it is often useful to assign each one a unique ID number so they can be identified. This is very easy to do with a static duration local variable:

The first time this function is called, it returns 0. The second time, it returns 1. Each time it is called, it returns a number one higher than the previous time it was called. You can assign these numbers as unique IDs for your objects. Because s_itemID is a local variable, it can not be “tampered with” by other functions.

Static variables offer some of the benefit of global variables (they don’t get destroyed until the end of the program) while limiting their visibility to block scope. This makes them much safer for use than global variables.

Quiz

1) What effect does using keyword “static” have on a global variable? What effect does it have on a local variable?

Quiz Answers

1) Show Solution

4.3a -- Scope, duration, and linkage summary
Index
4.2a -- Why global variables are evil

75 comments to 4.3 — Static duration variables

  • Hema

    Can you eplain this sentence in more detail-"Static duration variables are only created (and initialized) once, and then they are persisted throughout the life of the program."
    Thanks

    • Alex

      Sure. Normally, if you have a variable defined inside a function (either as a parameter or local variable), that variable is created and destroyed every time the function is executed. There’s no persistence. However, with a static variable, the static variable is only created the first time the function is run, and it is not destroyed when the function exits. The next time the function is run, the variable still exists and its previous value has been retained. The static variable is only destroyed at the end of the entire program.

  • felipe

    Is there a super static version of static(that makes the variable value persist even after the program is close?) thanks.

  • Kanwar Ujjwaldeep Singh

    VoidIncrementandPrint
    This Function has no return value,how it send’s back value 2 bact to MAIN FUNCTION.
    When main function calls VoidIncrementandPrint, Value is created ,assigned value =1,than incremented and then destroyed at end.But its not sent back to the main Function.Than how it works>

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter