Difference between Static and Dynamic Memory Allocation in C

Memory Allocation: Memory allocation is a process by which computer programs and services are assigned with physical or virtual memory space. The memory allocation is done either before or at the time of program execution. There are two types of memory allocations:

  1. Compile-time or Static Memory Allocation
  2. Run-time or Dynamic Memory Allocation

Static Memory Allocation: Static Memory is allocated for declared variables by the compiler. The address can be found using the address of operator and can be assigned to a pointer. The memory is allocated during compile time.

Dynamic Memory Allocation: Memory allocation done at the time of execution(run time) is known as dynamic memory allocation. Functions calloc() and malloc() support allocating dynamic memory. In the Dynamic allocation of memory space is allocated by using these functions when the value is returned by functions and assigned to pointer variables.

Tabular Difference Between Static and Dynamic Memory Allocation in C:


Static Memory Allocation

Dynamic Memory Allocation

1 In the static memory allocation, variables get allocated permanently. In the Dynamics memory allocation, variables get allocated only if your program unit gets active.
2 Static Memory Allocation is done before program execution. Dynamics Memory Allocation is done during program execution.
3 It uses stack for managing the static allocation of memory It uses heap for managing the dynamic allocation of memory
4 It is less efficient It is more efficient
5 In Static Memory Allocation, there is no memory re-usability In Dynamics Memory Allocation, there is memory re-usability and memory can be freed when not required
6 In static memory allocation, once the memory is allocated, the memory size can not change. In dynamic memory allocation, when memory is allocated the memory size can be changed.
7 In this memory allocation scheme, we cannot reuse the unused memory. This allows reusing the memory. The user can allocate more memory when required. Also, the user can release the memory when the user needs it.
8 In this memory allocation scheme, execution is faster than dynamic memory allocation. In this memory allocation scheme, execution is slower than static memory allocation.
9 In this memory is allocated at compile time. In this memory is allocated at run time.
10 In this allocated memory remains from start to end of the program. In this allocated memory can be released at any time during the program.
11 Example: This static memory allocation is generally used for array. Example: This dynamic memory allocation is generally used for linked list.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :