Search

4.3a — Scope, duration, and linkage summary

The concepts of scope, duration, and linkage cause a lot of confusion, so we’re going to take an extra lesson to summarize everything.

Scope summary

A identifier’s scope determines where it is accessible. An identifier that is out of scope can not be accessed.

  • Variables with block scope / local scope can only be accessed within the block in which they are declared. This includes:
    • Local variables
    • Function parameters
  • Variables with global scope / file scope can be accessed anywhere in the file. This includes:
    • Global variables

Duration summary

A variable’s duration determines when it is created and destroyed.

  • Variables with automatic duration are created at the point of definition, and destroyed when the block they are part of is exited. This includes:
    • Normal local variables
  • Variables with static duration are created when the program begins and destroyed when the program ends. This includes:
    • Global variables
    • Static local variables
  • Variables with dynamic duration are created and destroyed by programmer request. This includes:
    • Dynamically allocated variables (we’ll talk about these when we cover dynamic allocation in chapter 6)

Linkage summary

An identifier’s linkage determines whether multiple instances of an identifier refer to the same identifier or not.

  • Identifiers with no linkage mean the identifier only refers to itself. This includes:
    • Normal local variables
    • User-defined types, such as enums, typedefs, and classes declared inside a block (we’ll cover these in later lessons).
  • Identifiers with internal linkage can be accessed anywhere within the file it is declared. This includes:
    • Static global variables (initialized or uninitialized)
    • Const global variables
    • Static functions (we’ll cover these in chapter 7)
  • Identifiers with external linkage can be accessed anywhere within the file it is declared, or other files. This includes:
    • Normal functions
    • Non-const global variables (initialized or uninitialized)
    • Extern const global variables
    • User-defined types, such as enums, typedefs, and classes declared in the global scope (we’ll cover these in later lessons).

You can use a forward declaration to access a function in another file.
You can use the extern keyword to access a variable with external linkage in another file.

Variable scope, duration, and linkage summary

Because variables have scope, duration, and linkage, let’s summarize in a chart:

Type Example Scope Duration Linkage Notes
Local variable int x; Block scope Automatic duration No linkage
Static local variable static int x; Block scope Static duration No linkage
Dynamic variable int *x = new int; Block scope Dynamic duration No linkage
Function parameter void foo(int x) Block scope Automatic duration No linkage
Non-const global variable int g_x; File scope Static duration External linkage Initialized or uninitialized
Static global variable static int g_x; File scope Static duration Internal linkage Initialized or uninitialized
Const global variable const int g_x(1); File scope Static duration Internal linkage
Extern const global variable extern const int g_x(1); File scope Static duration External linkage

Functions and types don’t have scope or duration, so see the linkage summary above for more information on those.

4.3b -- Namespaces
Index
4.3 -- Static duration variables

16 comments to 4.3a — Scope, duration, and linkage summary

  • Got cleared all of my former doubts…Bro Thankyou so much for this awsome site…

  • Robert Hannah

    Wow. I was literally just about to write some kind of summary like this to supplement notes I’ve taken from your website. Thanks! I’ve been using this site for years: Perhaps the best non-book resource that there is on C++.

  • Hi, I have one doubt.
    is it possible to access a static local variable in a function from the main function. if it possible to access, then that data is safe or not?

    • Alex

      No, it is not possible (at least not directly). Static local variables are only visible within the function in which they are declared.

  • Hi Alex,
    Thanks a lot for the replay

    I tried indirectly, Like address of a static local variable assigned to a global pointer and deferred at another function. Compiler not showing any error and I am getting the expected result as well. As per my knowledge the lime time of the static local variable till up to the end of the program ,  is it correct? then why can’t access the local static variable outside that function(indirectly)? is any risk involved in this way like data overwriting?

    • Alex

      Yes, the lifetime of a local static variable is until the end of the program. But the scope of a local static variable is still only within the function in which it is declared, the same as a local non-static variable. The scope governs where you can access the variable, which is why you can’t use it directly outside the function.

      You can access the variable outside the function indirectly (via a pointer or reference), but even though you can do this, it’s not advised.

  • Thanks Alex,
    I need to clear one more thing, Consider I have 2 Task  and I called a common function from both the task which having a static local variable , So from one task I am accessing and set one value to that static variable, and through another task I read that value. So In both case am accessing the same local static variable. My doubt is ,am accessing the same memory ? able to consider that variable as a shared variable or shared memory?

    • Alex

      In both cases, you’re accessing the same local static variable, which uses the same memory. It’s not really shared memory -- it’s just a variable that you can access whenever you’re inside the function (doesn’t matter which function it was called from).

      • Hi Alex

        Why it’s not shared memory? As per my knowledge shared memory is the memory in which a common memory get accessed from 2 different task, is it correct ?  Would you please give me the definition of Shared memory?

        • Alex

          Maybe I’m misunderstanding what you mean by task. If by task you mean “process” or “thread” (or anything else that can run in parallel), then yes, the memory would be shared.

          • Yes, I meant Task as two separate Process that may or may not be run parallel.

            Now everything is clear

            Thanks Alex.

            The effort you put in this site is really worth for learner

  • kris

    This is very helpful. Thank you

  • Matt

    In section "Duration summary", the listings for automatic duration and static duration (local variables) should be updated to reflect that the variables are created during definition, not when the block is entered.

  • John

    Can you please explain how "Extern const global variable"  has external linkage ? can we access any variable defined as const in other file without using namespace?

    a.cxx:
    extern const int a(5);

    b.cxx:

    extern const int a;
    int main(){
        std::cout << a << std::endl;
    }

    It gives an error as “b.cxx:(.text+0x6): undefined reference to `a’ “

Leave a Comment

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