C Storage Duration Complete Tutorial

In this section, we will learn about the storage duration in C.

What is Storage Duration in C?

Storage duration is about how long the content of a variable will exist in the memory.

Example:

#include <stdio.h>
static void printValue(void);

int main() {

    for (int i = 0; i<5; i++){
        printValue();
    }
    return  0;
}

void printValue(void){
    int i = 900;

    printf("%d\n",i++);
}

Output:

900

900

900

900

900

Within the body of the function `printValue()` we’re trying to increase the value of the variable `i` by 1.

Then in the `main` function we called this `printValue` function 5 times. But the value of the `i` variable at each call is nothing but its initial, which is 900.

This is because the lifetime of the `i` variable is limited to the block of the `printValue` function.

This means, any time the function is called, the variable is defined and a memory space is set aside for it; but when the function execution finished, the variable is destroyed and its memory space will be taken back.

This process happens every time we call the function.

Storage Classes in C

Depending on where and how we define a variable, it will have different types storage duration.

For example, a variable that is declared outside of any function has the storage class of static, which it means the variable will stay in the memory as long as the program is running.

These types of storage are categorized in 4 classes:

Types of Storage Classes

  • Static Storage
  • Automatic Storage
  • Register Storage
  • Thread Storage

Static Storage

A variable that is declared outside of any function or its memory space is allocated dynamically via the call to functions like `malloc` etc. has the static storage.

Such variable will stay in the memory as long as the program is running.

Note: for dynamically allocated memories, the lifetime of the variable is linked to when we call the `free` function to release the memory-space and basically destroy the variable.

Example: creating static storage variable in C

#include <stdio.h>
void printValue(void);

int i = 100;
int main() {

    for (int i = 0; i<5; i++){
        printValue();
    }
    return  0;
}

void printValue(void){
    printf("%d\n",i++);
}

Output:

100

101

102

103

104

As you can see, the variable `i` in this example has the static-storage duration and so it will stay in the memory no-matter how many times we call a function that works with this variable.

Also, we can change the storage duration of a local variable (that is declared inside a function or a block) into static duration by adding the `static` specifier on the left side of the variable when it is being declared.

Example:

#include <stdio.h>

void printValue(void);
int main() {

    printValue();
    printValue();
    printValue();
    return  0;
}

void printValue(void){
    static int ii = 10; 
    println("%d\n",ii++); 
}

Output:

10

11

12

Within the body of the `printValue()` function we have declared the `ii` variable with the `static` specifier and as a result this variable will be declared and initialized once the function is called for the first time, but for the next times there will be no declaration anymore.

For this example, after the first call to the `printValue()` function, the next times we call it, it will simply increase the value of the `ii` variable by one because of `ii++` statement.

Automatic Storage

Any variable that is created with block-scope has the automatic-storage and will stay in the memory as long as its block is running. The moment the execution is out of the block; the variable will be destroyed as if it didn’t exist at all.

Example: creating automatic storage variable in C

#include <stdio.h>
void printValue(void);

int main() {

    for (int i = 0; i<5; i++){
        printValue();
    }
    return  0;
}

void printValue(void){
    int i = 10;
    printf("%d\n",i++);
}

Output:

10

10

10

10

10

The variable `i` here in this example is a local variable that exists within the body of `printValue` function. So the variable only exists as long as the function `printValue` is running. The moment this function is done running, so the variable `i` that is in the function.

That’s why every time we called the function, the same variable is created and the same value is sent to the output.

Register Storage

“Variables are normally stored in computer memory. With luck, register variables are stored in the CPU registers or, more generally, in the fastest memory available, where they can be accessed and manipulated more rapidly than regular variables. Because a register variable may be in a register rather than in memory, we can’t take the address of a register variable. In most other respects, register variables are the same as automatic variables. That is, they have block scope, no linkage, and automatic storage duration.

We say “with luck” because declaring a variable as a register class is more a request than a direct order. The compiler has to weigh your demands against the number of registers or amount of fast memory available or it can simply ignore the request, so you might not get your wish. In that case, the variable becomes an ordinary automatic variable; however, you still can’t use the address operator with it.” This part was taken from the “C Primer Plus, 6th Edition” book.

Example: creating register storage variable in C

#include <stdio.h>
void printValue(void);

int main() {

    for (int i = 0; i<5; i++){
        printValue();
    }
    return  0;
}

void printValue(void){
    register int i = 10;
    printf("%d\n",i++);
}

Output:

10

10

10

10

10

Thread Storage

Thread storage duration comes into play in concurrent programming, in which a program execution can be divided into multiple threads. An object with thread storage duration exists from the time it is declared within a thread until the thread terminates. You will learn more about this in later sections.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies