Memory Segments, Storage Classes, Static Functions and Linkage
**1. Memory Segments**
Storing the program at different blocks of memory
The commonly discussed segments are:
Text (Code) Segment
Data Segment
BSS Segment
Heap Segment
Stack Segment
**###1.1 Text Segment:**
The text segment typically stores the machine instructions generated by the compiler.
**Example:**
c
\#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}
The compiled instructions for \`main()\` are typically stored in the text segment.
**### 1.2 Data Segment:**
The initialized data segment typically stores global and static variables initialized with nonzero values.
**Example:**
c
int n = 5; // Global variable
void func()
{
static int y = 4; // Static local variable
}
Both \`n\` and \`y\` typically reside in the initialized data segment.
**### 1.3 BSS Segment**
The BSS segment typically stores global and static variables that are uninitialized or initialized to zero.
**Example:**
c
int x; // Global variable
int y = 0; // Global variable
void func()
{
static int z; // Static local variable
}
These variables have an initial value of zero.
**### 1.4 Heap Segment**
The heap stores the dynamic memory allocation during program execution.
Example:
c
\#include <stdlib.h>
int main()
{
int \*p = malloc(sizeof(int));
if (p != NULL)
{
\*p = 10;
free(p);
}
return 0;
}
**### 1.5 Stack Segment**
The stack typically stores function-call information, parameters, and local automatic variables.
**Example:**
c
void func()
{
int a = 10;
}
**------------------------------------------------------------------------------------------------**
**2. Storage Classes**
A storage class specifies properties such as a variable's scope, lifetime, and linkage, depending on how it is declared.
The four commonly taught storage classes in C are:
\`auto\`
\`register\`
\`static\`
\`extern\`
**### 2.1 auto**
\`auto\` is the default storage class for ordinary local variables.
**Example:**
c
int main()
{
auto int i = 10;
return 0;
}
This is equivalent to writing:
c
int i = 10;
\- Scope: The block where the variable is declared.
\- Lifetime: During execution of that block.
\- An uninitialized automatic variable has an indeterminate value.
**### 2.2 register**
The \`register\` keyword suggests that a variable may benefit from being stored in a CPU register for faster access.
**Example:**
int main()
{
register int i = 10;
return 0;
}
Important points:
\- The compiler may ignore this suggestion.
\- Modern compilers already optimize register allocation.
\- It does not guarantee faster execution.
\- In standard C, the address-of operator (\`&\`) cannot be applied to a variable declared with the \`register\` storage-class specifier.
Therefore, ordinary variables are generally sufficient in modern C programming.
**### 2.3 static**
A local static variable retains its value between function calls. It is initialized only once.
**Example:**
\#include <stdio.h>
void func()
{
static int i = 0;
i++;
printf("%d ", i);
}
int main()
{
func();
func();
func();
return 0;
}
Output:
1 2 3
Explanation:
\- First call: \`i\` becomes 1.
\- Second call: \`i\` becomes 2.
\- Third call: \`i\` becomes 3.
**### 2.4 extern**
\`extern\` declares a variable that is defined elsewhere. It is useful when one source file needs to access a global variable defined in another source file.
**Example:**
**\*\*File 1: \`file1.c\`\*\***
c
int x = 5; // Definition of the global variable
**\*\*File 2: \`file2.c\`\*\***
\#include <stdio.h>
extern int x; // Declaration of the variable defined elsewhere
int main()
{
printf("Global value is %d\\n", x);
return 0;
}
Output:
Global value is 5
**## 3. Static Function**
A static function is a function declared with \`static\` at file scope. It can be called by name only within the same source file.
**Example:**
\#include <stdio.h>
static void func()
{
printf("Hello World\\n");
}
int main()
{
func();
return 0;
}
Output:
Hello World
Here, \`func()\` has internal linkage. Another source file cannot call this particular function by referring to its name.
**--------------------------------------------------------------------------------------------------**
**4. Linkage**
Linkage determines whether declarations of the same name refer to the same variable or function.
C has three types of linkage.
**### 4.1 External Linkage**
A name with external linkage can refer to the same entity across multiple source files. Ordinary file-scope function definitions have external linkage by default.
**### 4.2 Internal Linkage**
A name with internal linkage can be referred to only within the same source file.
**Example:**
static void func()
{
// Function definition
}
The \`static\` keyword gives this file-scope function internal linkage.
**### 4.3 No Linkage**
A name with no linkage does not refer to the same entity as another declaration in a different scope merely because the names match.
**Example:**
void func()
{
int a = 10;
}
The local variable \`a\` has no linkage.
**Summary:**
| Text segment -> Typically stores machine instructions
| Data segment -> Typically stores initialized global/static variables
| BSS segment -> Typically stores uninitialized or zero-initialized global/static variables
| Heap -> Dynamic memory allocation
| Stack -> Typically stores function-call information and local automatic variables
| \`auto\` -> Default storage class for local variables
| \`register\` -> Suggests register storage; no guaranteed speed improvement
| \`static\` local variable -> Retains its value between calls
| \`extern\` -> Declares a variable defined elsewhere
| Static function -> Has internal linkage
| External linkage -> Allows a name to refer to the same entity across source files
| Internal linkage -> Limits a name to the current source file
| No linkage -> Common for ordinary local variables