Search Bar

Saturday, February 18, 2017

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 (via a forward declaration). 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).
Identifiers with external linkage will cause a duplicate definition linker error if the definitions are compiled into more than one .cpp file.
There are a few things worth explicitly noting here. First, functions are extern by default, and can be made internal by using the static keyword.
Second, astute readers may note that global types have external linkage, but their definitions don’t cause a linker error when included in multiple files. This is because types, templates, and extern inline functions have an exemption that allows them to be defined in more than one file, so long as the definitions are identical. Otherwise, they wouldn’t be of much use.
Variable scope, duration, and linkage summary
Because variables have scope, duration, and linkage, let’s summarize in a chart:
TypeExampleScopeDurationLinkageNotes
Local variableint x;Block scopeAutomatic durationNo linkage
Static local variablestatic int x;Block scopeStatic durationNo linkage
Dynamic variableint *x = new int;Block scopeDynamic durationNo linkage
Function parametervoid foo(int x)Block scopeAutomatic durationNo linkage
External non-const global variableint g_x;File scopeStatic durationExternal linkageInitialized or uninitialized
Internal non-const global variablestatic int g_x;File scopeStatic durationInternal linkageInitialized or uninitialized
Internal const global variableconst int g_x(1);File scopeStatic durationInternal linkageMust be initialized
External const global variableextern const int g_x(1);File scopeStatic durationExternal linkageMust be initialized
Forward declaration summary
You can use a forward declaration to access a function or variable in another file:
TypeExampleNotes
Function forward declarationvoid foo(int x);Prototype only, no function body
Non-const global variable forward declarationextern int g_x;Must be uninitialized
Const global variable forward declarationextern const int g_x;Must be uninitialized

No comments:

Post a Comment

whatiscpp.blogspot.com is a free website devoted to teaching you how to program in C++. Whether you’ve had any prior programming experience or not, the tutorials on this site will walk you through all the steps to write, compile, and debug your C++ programs, all with plenty of examples.