Computer programs and services are given physical or virtual memory space through the memory allocation mechanism. Either before or during the execution of the program, memory is allocated. When you assign values to variables in programming, memory allocation is crucial for storing those values. This ultimately allows memory for the variables that a programmer has declared through the compiler. Memory allocations come in two flavours:
- Static Memory Allocation
- Dynamic Memory Allocation.
The memory is divided into three sections:
- Heap memory – It is a portion of the main memory, or the heap memory. It is not organised and handled as a resource when you need to use or release it. A pointer cannot be used to access heap memory directly.
- Stack Memory: This is where function-created temporary variables are kept. Variables in a stack are created, saved, and initialised while the program is running. It adheres to the First in, Last out principle, which states that whatever element is stored last will be deleted first when it is no longer needed.
- Code Section: The program will be loaded into the main memory each time it is run. The code area will be where this software is saved. Based on the program, it will decide whether to use the stack or the heap.
Static Allocation
All data objects are allocated using the static allocation technique at the compilation time. Allocation of data objects solely occurs in this sort of allocation at build time. It is simple for the compiler to determine later the addresses of these data objects in the activation records. FORTRAN implements static allocation.
Heap Allocation
The management of memory allocation is done through a process known as heap allocation. Heap facilitates the control of dynamic memory allocation. Like stack allocation, dynamic data objects and structures can likewise be created using heap allocation. The restriction of stack allocation is addressed using heap allocation.
What is Static Memory Allocation?
The compiler allows static memory for defined variables. The address can be located using the operator’s address and given to a pointer. Memory is allocated during the compilation process.
Advantages:
- Easy to use.
- The compiler handles allocation and deallocation.
- efficient time for execution.
- Stack data structures are employed.
Disadvantage:
- Problem with memory loss is a drawback.
- The precise memory needs must be known.
- After initialization, memory cannot be resized.
What is Dynamic Memory Allocation?
Dynamic memory allocation is memory allocation carried out at the moment of execution (run time). Allocating dynamic memory is supported by the calloc() and malloc() functions. Dynamic memory space is allocated using these functions when a function returns a value, and a value is assigned to a pointer variable.
Advantages:
- Run-time dynamic allocation is carried out.
- When we need more space, we can assign or generate it.
- Dynamic space can be deallocated (released/deleted) whenever we are finished with them.
- As a result, one can always have the space needed — neither more nor less.
- If necessary, memory space can be reassigned.
Disadvantage:
- It takes more time because the RAM is allocated at runtime.
- When finished, the user must release memory. This is crucial because it increases the likelihood that bugs will develop that are challenging to find.
Difference Between Static And Dynamic Memory Allocation In C
Sr. No | Static Memory Allocation | Dynamic Memory Allocation |
1 | When the allocation of memory performs at the compile time, then it is known as static memory. | When the memory allocation is done at the execution or run time, it is called dynamic memory allocation. |
2 | The memory is allocated at the compile time. | The memory is allocated at the runtime. |
3 | In static memory allocation, the memory cannot be changed while executing a program. | In dynamic memory allocation, while executing a program, the memory can be changed. |
4 | Static memory allocation is preferred in an array. | Dynamic memory allocation is preferred in the linked list. |
5 | It saves running time as it is fast. | It is slower than static memory allocation. |
6 | Static memory allocation allots memory from the stack. | Dynamic memory allocation allots memory from the heap. |
7 | Once the memory is allotted, it will remain from the beginning to end of the program. | Here, the memory can be alloted at any time in the program. |
8 | Static memory allocation is less efficient as compared to Dynamic memory allocation. | Dynamic memory allocation is more efficient as compared to the Static memory allocation. |
9 | This memory allocation is simple. | This memory allocation is complicated. |