Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Static Memory Allocation?
2.1.
Example
2.2.
C
3.
What is Dynamic Memory Allocation?
3.1.
Example
3.2.
C
4.
Difference between Static and Dynamic Memory Allocation
5.
Frequently Asked Questions
5.1.
How can we allocate memory dynamically?
5.2.
What is the difference between calloc() and malloc()?
5.3.
What are some similarities between static and dynamic memory allocation?
5.4.
Which memory allocation should be used for arrays and why?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Difference between Static and Dynamic Memory Allocation in C

Author Rhythm Jain
3 upvotes
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Before knowing about Static or Dynamic memory allocation, let us learn about Memory Allocation itself. 

Memory allocation is allocating physical or virtual memory space to computer programs and services.  It is divided into two types based on memory allocation occurring before or during program execution:

  1. Static Memory Allocation
  2. Dynamic Memory Allocation
Difference Between Static and Dynamic Memory Allocation

The choice between static and dynamic memory allocation is determined by the program's specific requirements. Static memory allocation is appropriate when the variable's size is fixed, known in advance, and does not need to be changed during runtime. Dynamic memory allocation is chosen when the size of the memory required is decided at runtime or when memory must be allocated and deallocated dynamically during program execution.

Let’s learn more about each of them and practice it on the online editor.

What is Static Memory Allocation?

Static Memory Allocation in C refers to the allocation of memory for variables at compile-time, and the memory size is determined before the program execution. This means that the compiler assigns memory addresses to variables during the compilation phase, and the allocated memory remains constant throughout the program's execution.

Example

  • C

C

#include <stdio.h>
int main()
{
  int a;
  int b[10];
  return 0;
}

Here memory allocation is done during compile time and is Static Memory Allocation.

Also See, Sum of Digits in C and C Static Function

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is Dynamic Memory Allocation?

Dynamic Memory Allocation in C involves allocating memory for variables during program execution, allowing flexibility in size and utilization. It is achieved using functions like malloc(), calloc(), realloc(), and requires explicit deallocation using free() to prevent memory leaks.

Example

  • C

C

#include <stdio.h>
#include <stdlib.h>
int main()
{
  int *a=(int*)calloc(10*sizeof(int),0);
  int *b=(int*)malloc(10*sizeof(int));
 
  free(a);
  free(b);
  return 0;
}

Memory is allocated dynamically using malloc() and calloc() keywords. Memory allocated dynamically must be released using the free() keyword. Here we created two integer arrays dynamically.

You can also read about the dynamic arrays in c, And Tribonacci Series

Difference between Static and Dynamic Memory Allocation

Parameter Static Memory Allocation Dynamic Memory Allocation
Execution Timing Static Memory allocation is done before the execution of a program. Dynamic Memory Allocation is done during the Execution of program.
Memory Allocation Condition In Static Memory allocation, memory of the variables are allocated permanently until the program or function call completes. Dynamic Memory Allocation the variables' memory is allocated only when required and called by the calloc()/malloc() function.
Memory Source Static Memory Allocation is done from the Stack Memory. Dynamic Memory Allocation is done from the Heap Memory.
Memory Size Flexibility In Static memory, if the memory is allocated for a program, the memory size cannot be changed. In Dynamic memory, if the memory is allocated for a program, the memory size can be changed later.
Memory Efficiency Static Memory Allocation is less Efficient Memory Management. Dynamic Memory Allocation is more Efficient memory management..
Memory Reusability Static Memory allocated cannot be reused. Dynamic Allocated memory can be released and used again if not required anymore.
Execution Speed In Static Memory Allocation, execution of the program is faster than when the memory is allocated dynamically. In Dynamic Memory Allocation, execution of the program is slower than when the memory is allocated statically..
Memory Management Complexity Static Memory Allocation is simple. Dynamic Memory Allocation is complex.
Memory Lifetime Static Memory declared stays from the beginning to the end of the program execution. Dynamic Memory declared can be freed ans reused, and other memory can be allocated.
Data Structure Usage Static memory allocation is used for arrays Dynamic memory allocation is used for Linked-Lists

You can also read about the memory hierarchy.

Frequently Asked Questions

How can we allocate memory dynamically?

We can use the calloc() or malloc() keyword to allocate memory dynamically. Calloc() takes 2 arguments as input, “number_of_blocks” and “size_of_each_block” whereas malloc() takes only one argument “number_of_bytes”.

What is the difference between calloc() and malloc()?

The only difference between calloc() and malloc() is that while malloc doesn’t initialize the memory space so with malloc(), memory has garbage value, but with calloc(), memory is initialized to 0.

What are some similarities between static and dynamic memory allocation?

Static and dynamic memory allocation both involve the management of computer memory. They share similarities in facilitating variable storage, allowing program data manipulation, and necessitating proper deallocation to avoid memory leaks and inefficiencies.

Which memory allocation should be used for arrays and why?

Static memory allocation should be used for Arrays because it is fast and allocated in contiguous memory locations.

Conclusion

Today, we learned about Static and Dynamic Memory allocations and how they differ from each other in terms of efficiency, usage, reusability, complexity, and allocation. We also discussed about them from an example describing how both static and dynamic memory is allocated. 

Also check out - Buddy System In OS  Bubble Sort Program in C and Data Structure

To learn about different types of storage classes and functions, you can visit Storage Class blog by coding ninjas.

Happy Coding

Previous article
Dereference Pointers
Next article
Why does C treat array parameters as pointers?
Live masterclass