In programming, it is necessary to store computational data. These data are stored in memory. The memory locations for storing data in computer programming is known as variables. The variables have a specific data type. Therefore, the memory is allocated to run the programs. Memory can be allocated in two ways. They are Static memory allocation and Dynamic memory allocation. In static memory allocation, once the memory is allocated it cannot be changed. The memory is not reusable. But in dynamic memory allocation, once the memory is allocated it can be changed. The key difference between static and dynamic memory allocation is that in static memory allocation once the memory is allocated, the memory size is fixed while in dynamic memory allocation, once the memory is allocated, the memory size can be changed.
CONTENTS
1. Overview and Key Difference
2. What is Static Memory Allocation
3. What is Dynamic Memory Allocation
4. Similarities Between Static and Dynamic Memory Allocation
5. Side by Side Comparison – Static vs Dynamic Memory Allocation in Tabular Form
6. Summary
What is Static Memory Allocation?
In static memory allocation, the allocated memory is fixed. Once the memory is allocated, it cannot be changed. The memory cannot be increased or decreased. For example, in C language if the programmer writes int x, which means that the variable can store an integer value. The number of bytes depends on the computer. There can also be arrays. E.g. int x [5]; This x is an array which can store a sequence of data which are of the same type. It can store five integer elements. It cannot store more than five elements. In Java, an array can be created as , int arr[] = new int[5]; The array ‘arr’ can store 5 integer values and cannot store more than that.
In static memory allocation, once the variables are allocated, they remain permanent. After the initial allocation, the programmer cannot resize the memory. If the programmer allocated an array that can store 10 elements, it is not possible to store values more than that specified amount. If the programmer initially allocated an array that can hold 10 elements, but only needed 5 elements, then there is a memory wastage. That memory is no longer needed, but it is also not possible to reuse the memory. Static memory allocation is fixed but the implementation is simple and easy, and it is also fast.
What is Dynamic Memory Allocation?
Sometimes it is necessary to change the size of the memory. So memory can be allocated dynamically. Depending on insertions and deletes of the data elements, the memory can grow or shrink. It is known as dynamic memory allocation.
In C language, stdlib.h header file, there are four functions for dynamic memory allocation. They are calloc, malloc, realloc and free. The function malloc() allocates a required size of bytes and returns a void pointer, pointing the first byte of the allocated memory. The function calloc() allocates a required size of bytes and initialize them to zero. Then returns a void pointer to the memory. The free() function is used to reallocate the allocated memory. And realloc function can modify the previously allocated memory. After allocating memory using calloc or malloc, the memory size is fixed, but they can be increased or decreased using the realloc function. In Java, collections can be used for dynamic memory allocation.
The main advantage of dynamic memory allocation is that it saves memory. The programmer can allocate memory or release the memory as necessary. Memory can be reallocated during execution and can free the memory when it is not required. Dynamic memory allocation is also efficient than static memory allocation. One disadvantage is that implementing dynamic memory allocation is complex.
What are the Similarities Between Static and Dynamic Memory Allocation?
- Both are memory allocation mechanisms.
- Both should be implemented by the programmer manually.
What is the Difference Between Static and Dynamic Memory Allocation?
Static vs Dynamic Memory Allocation |
|
Static memory allocation is a method of allocating memory, and once the memory is allocated, it is fixed. | Dynamic memory allocation is a method of allocating memory, and once the memory is allocated, it can be changed. |
Modification | |
In static memory allocation, it is not possible to resize after initial allocation. | In dynamic memory allocation, the memory can be minimized or maximize accordingly. |
Implementation | |
Static memory allocation is easy to implement. | Dynamic memory allocation is complex to implement. |
Speed | |
In static memory, allocation execution is faster than dynamic memory allocation. | In dynamic memory, allocation execution is slower than static memory allocation. |
Memory Utilization | |
In static memory allocation, cannot reuse the unused memory. | Dynamic memory allocation allows reusing the memory. The programmer can allocate more memory when required . He can release the memory when necessary. |
Summary – Static vs Dynamic Memory Allocation
In programming, Static memory allocation and dynamic memory allocation are two mechanisms for allocating memory. The difference between static and dynamic memory allocation is that in static memory allocation once the memory is allocated, the memory size is fixed while in dynamic memory allocation, once the memory is allocated, the memory size can be changed. The programmer can decide whether the memory should be static or dynamic depending on the application.
Download the PDF of Static vs Dynamic Memory Allocation
You can download PDF version of this article and use it for offline purposes as per citation note. Please download the PDF version here: Difference Between Static and Dynamic Memory Allocation
Reference:
1.Kayal, Somnath. “SOMNATH KAYAL.” Difference Between Static Memory Allocation & Dynamic Memory Allocation, 1 Jan. 1970. Available here
2.tutorialspoint.com. “Arrays in C.” The Point. Available here
3.nareshtechnologies. Static Memory vs Dynamic Memory | C Language Tutorial, Naresh i Technologies, 19 Sept. 2016. Available here