Scope, Duration, and Linkage

It is essential to understand how variables and functions behave in terms of scope, duration, and linkage. These three concepts define where variables and functions can be accessed (scope), how long they last in memory (duration), and whether they can be accessed across different files (linkage).

1️⃣ Scope

Scope defines the region of the program where a particular identifier, such as a variable or function, is accessible. Once you move out of the scope, you can no longer access that entity.

Types of Scope:

Block Scope (Local Scope):

  • Variables declared inside a block (e.g., {} within functions, loops, or conditionals) are only accessible within that block.
  • These variables disappear when the block ends.
  • They are known as local variables.
Example:
void myFunction() {
    int x = 10;  // x is only visible inside myFunction
    if (true) {
        int y = 20;  // y is only visible inside this if block
    }
    // y is no longer accessible here
}

Variables with block scope are local to the block in which they are declared.

Function Scope:

Variables declared within a function but outside of any block have function scope. They are visible throughout the entire function.

int globalVar = 20; // globalVar has function scope

int main() {
    // globalVar is accessible here
    // ...
}

File Scope (Global Scope):

  • Variables or functions declared outside of any function (global variables or functions) have file scope. They can be accessed anywhere in the file after they are declared.
Example:
// file scope variable
int globalVar = 30;

int main() {
    // globalVar is accessible here
    // ...
}

Class Scope (C++ only):

  • Members of a class in C++ have class scope. They can be accessed within the class using objects or methods, and access control is defined by access specifiers (private, protected, public).
Example:
class MyClass {
private:
    int privateVar;  // Only accessible within MyClass
public:
    void setVar(int val) { privateVar = val; }  // Access within class methods
};

2️⃣ Duration (Storage Duration)

Duration defines how long a variable stays in memory during program execution. It tells us how long the variable's value is retained.

Types of Duration

Automatic Duration (Local Variables):

  • Local variables declared inside a function or block (those with block scope) have automatic duration. They exist when the block is entered and disappear when the block is exited.
Example:
void myFunction() {
    int x = 10;  // x is created when the function is called
    // x exists while the function runs
}  // x is destroyed when the function ends
  • Key Point: Variables with automatic duration are created and destroyed automatically as the block or function starts and ends.

Static Duration

  • Variables with static duration exist for the entire duration of the program, even if they are declared inside a function or block. They are initialized only once and retain their value across function calls.
  • Use the static keyword to define such variables.
Example:
void myFunction() {
    static int counter = 0;  // counter is initialized only once
    counter++;  // Retains its value across multiple function calls
}
  • Key Point: Static variables are created once and live until the program ends, even if they are declared inside a function.

Dynamic Duration

  • Dynamically allocated variables (using new in C++ or malloc in C) exist in memory until you manually free them with delete or free. These variables are created during runtime and can be managed by the programmer.
Example:
int* p = new int(10);  // p points to a dynamically allocated integer
delete p;  // Free the memory manually
  • Key Point: You are responsible for managing the memory of dynamically allocated variables.

Thread Storage Duration (C++11 and later)

  • Variables declared with the thread_local keyword have thread storage duration. Each thread gets its own copy of the variable, and the variable exists as long as the thread does.
Example:
thread_local int threadVar = 0;  // Each thread has its own copy of threadVar

3️⃣ Linkage

Linkage controls whether a variable or function can be accessed from other files or translation units in a multi-file program.

Types of Linkage:

No Linkage

  • Variables with no linkage are local variables declared inside a block (e.g., inside a function) and cannot be accessed from outside that block.
Example:
void myFunction() {
    int x = 10;  // x has no linkage, it is local to this function
}
  • Key Point: Local variables inside a function have no linkage, meaning they are not visible outside the function.

Internal Linkage

  • Variables or functions with internal linkage are visible only within the file where they are declared. This is achieved using the static keyword.
Example:
static int internalVar = 100;  // internalVar is only accessible in this file
  • Key Point: Internal linkage restricts access to variables or functions from outside the current file.

External Linkage

  • Variables or functions with external linkage can be accessed from other files. Global variables and functions by default have external linkage unless specified otherwise.
Example:
// File1.cpp
int globalVar = 42;  // globalVar has external linkage by default

// File2.cpp
extern int globalVar;  // Declare the variable from File1.cpp
  • Key Point: External linkage allows variables and functions to be accessed across multiple files in a program.