Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is Lifetime in C?

Amaaz Ahmad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The lifetime of a variable (also know as the storage duration of the variable) determines the duration for which the variable will stay valid in the memory before it is destroyed. The storage class of the variable specifies the lifetime.

It is important to note that the scope and the lifetime of a variable are different. You can find more information about this difference here.

In C, a variable can only have one of the following three storage durations:

  1. static
  2. automatic
  3. dynamic

Static

Creation

The compiler only creates a static when it executes the variable’s declaration statement for the first time. The following snippet of code creates a static integer:

static int num = 0;

If the declaration statement is executed more than once, the compiler ignores all successive calls and does not change the variable’s value.

Storage

A static variable is stored in the data segment of the object file of a program.

Lifetime

A static variable remains valid until the program runs, even if the parent block of code has finished executing.

Example

In the following snippet of code, a static variable is created in the memory when the declaration statement (line 5) executes for the first time, i.e., during the first function call. When the function ends, the static variable is not deleted from the memory because its lifetime is valid until the program is running.

When the function is executed for the second time, the declaration statement is ignored and the variable’s value remains unchanged. We can see this here:

#include<stdio.h>
void incrementAndPrint() {
// declaring and initialising a static variable
static int num = 2;
// incrementing in the number
num = num + 2;
//printing the static variable
printf("Number: %d \n", num);
}
void main() {
// calling the function twice to observe the behaviour of static variable
incrementAndPrint();
incrementAndPrint();
}

Automatic

Creation

An automatic variable (also known as a local variable) is created when the program enters its parent block of code. The following code snippet creates a local variable inside its parent function:

int num = 0;

If the declaration statement is repeated, the compiler does not ignore it and throws an error.

Storage

A local variable is stored in the function call stack of a program.

Lifetime

An automatic variable remains valid until its parent block of code is running. The variable is removed from the memory when the parent block stops executing.

Example

A variable is created inside a function in the next snippet of code. This function is called twice.

The following steps will take place with every function call:

  1. The variable is created.
  2. The variable’s value is incremented by 2.
  3. The variable’s value is printed.
  4. The variable is destroyed from the memory.

This is why the program prints the value 4 twice (once in each function call). Take a look at the code below.

#include<stdio.h>
void incrementAndPrint() {
// declaring and initialising a variable
int num = 2;
// incrementing in the number
num = num + 2;
//printing the variable
printf("Number: %d \n", num);
}
void main() {
// calling the function twice to observe the behaviour of the variable
incrementAndPrint();
incrementAndPrint();
}

Dynamic

Creation

A dynamic variable is created when we allocate the variable’s memory. We use a pointer and functions like calloc and malloc to allocate the dynamic memory to a variable. Below is an example of a declaration statement for a dynamic variable:

int* ptr = (int*) malloc(sizeof(int));

If we want to create another dynamic variable, we will have to use another pointer. If the same pointer is used, an error is generated.

Storage

A dynamic variable is stored on the heap of the program.

Lifetime

A dynamic variable is only removed from the heap when deallocating it using the free function. If we do not deallocate the memory and the program ends, it is called a memory leak.

Example

The following snippet of code creates a dynamic variable using the malloc function, prints its original and the incremented value, and deallocates the memory:

#include<stdio.h>
#include<stdlib.h>
int main() {
// creating a pointer and allocating dynamic memory
int* ptr = (int*) malloc(sizeof(int));
// setting the variable's value
*ptr = 10;
// printing the variable's value
printf("Value of *ptr: %d \n", *ptr);
// incrementing the variable's value
(*ptr)++;
// printing the incremented value
printf("Value of *ptr: %d \n", *ptr);
// deallocating the dynamic memory allocated to the pointer
free(ptr);
}

RELATED TAGS

c

CONTRIBUTOR

Amaaz Ahmad
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring