Search

S.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. Some of these things we haven’t covered yet, and they’re here just for completeness / reference later.

Scope summary

An 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 (including nested blocks). This includes:
    • Local variables
    • Function parameters
    • Locally-defined types
  • Variables and functions with global scope / file scope can be accessed anywhere in the file. This includes:
    • Global variables
    • Regular functions (the kind we’ve discussed so far)
    • Globally-defined types

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:
    • 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:
    • 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
  • Identifiers with external linkage can be accessed anywhere within the file it is declared, or other files (via a forward declaration). This includes:
    • Regular functions (the kind we’ve discussed so far)
    • 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).

Identifiers with external linkage will generally cause a duplicate definition linker error if the definitions are compiled into more than one .cpp file (due to violating the one-definition rule). There are some exceptions to this rule (for types, templates, and extern inline functions) -- we’ll cover these in future lessons when we talk about those topics.

Also note that functions are extern by default. They can be made internal by using the static keyword.

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 s_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
External non-const global variable int g_x; File scope Static duration External linkage Initialized or uninitialized
Internal non-const global variable static int g_x; File scope Static duration Internal linkage Initialized or uninitialized
Internal const global variable const int g_x(1); File scope Static duration Internal linkage Must be initialized
External const global variable extern const int g_x(1); File scope Static duration External linkage Must be initialized

Forward declaration summary

You can use a forward declaration to access a function or variable in another file:

Type Example Notes
Function forward declaration void foo(int x); Prototype only, no function body
Non-const global variable forward declaration extern int g_x; Must be uninitialized
Const global variable forward declaration extern const int g_x; Must be uninitialized
S.4.3b -- Namespaces
Index
S.4.3 -- Static duration variables

80 comments to S.4.3a — Scope, duration, and linkage summary

  • Anastasia

    Hi,
    I may be dumb, but in the linkage summary it's said that static functions are covered in chapter 7, but I can't see where it is and don't remember learning about them (except for member static functions, which I suppose are a totally different thing and they are covered later). Can anyone point me in the right direction?

    • Alex

      Hmmm, I think I actually don't cover these explicitly. I've removed the reference to chapter 7.

      But just like static global variables, static functions are only accessible in the file in which they are declared.

      • Anastasia

        There's a brief explanation (with an example) of static functions in lesson S.4.2 — Global variables and linkage (paragraph 'Function linkage'), it could be used as a reference instead (as a little refresher).

        Thanks!

  • mmp52

    Hello!
    On the end of the course, you've said that when we make forward declaration of a constant global variable in order to use it in another file, it should not be initialized. Shouldn't we initialize all constant variables in the moment of definition? What should I use if I want a constant global variable with external linkage and I want it to be initialized during definition?

    Thank you!

    • Alex

      Forward declarations just tell the compiler that an object (or function) exists (and what type it is). They don't define actual objects, and so they can't have initializers.

      The actual defined object (that the forward declaration is referencing) should be initialized.

  • SM.Haider

    Wait, what's the scope of this article?

  • Gabe

    "Identifiers with external linkage will generally cause a duplicate definition linker error if the definitions are compiled into more than one .cpp file (due to violating the one-definition rule)."

    Would you mind explaining this sentence? What does "if the definitions are compiled into more than one .cpp file"?

    • gabe.hpp

      gabe.cpp

      main.cpp

      `gabe` is now defined in "gabe.cpp" and "main.cpp" (Because `#include` simply copies a file's content). Variables with external linkage can only be defined once, so you're getting and error.

  • Samira Ferdi

    Hi, Alex and Nascardriver!

    What is normal function? I confused the term 'normal'. So, are there functions those are not normal?

    • Alex

      Normal functions are the kind of functions we've presented so far. There are other kinds of functions (member functions, inline functions, etc...) that you'll learn about later.

Leave a Comment

Put all code inside code tags: [code]your code here[/code]