Do you think IIT Guwahati certified course can help you in your career?
No
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:
Static Memory Allocation
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; }
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));
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.
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.