Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Memory allocation refers to assigning a particular space in a computer's memory for a computer program or code.
There are two types of memory allocations.
Static Memory allocation
Dynamic Memory allocation
The key difference between Static and Dynamic memory allocation is that Static memory allocation allows fixed memory size after allocation while Dynamic memory allocation allows changes in the memory size after allocation.
What is Static Memory allocation?
Static memory allocation is also known as Compile-time memory allocation because the memory is allocated during compile time. In this type of memory allocation, the memory that the program can use is fixed i.e. we can not allocate or deallocate memory during the program's execution. In many applications, it is not possible to predict how much memory will be needed by the program at run time.
Properties of Static Memory allocation
Memory allocation is done during compile time.
Stack Memory is used here.
Memory cannot be changed while executing a program.
The static memory allocation is fast and saves running time.
It is less efficient as compared to Dynamic memory allocation.
Memory cannot be changed while executing a program.
Exact memory requirements must be known.
If memory is not required, it cannot be freed.
What is Dynamic Memory allocation?
Dynamic memory allocation is also known as Runtime memory allocation because the memory is allocated during runtime or program execution. The allocation and release of the memory space can be done using the library functions of stdlib.h header file. These functions allocate memory from a memory area called heap and deallocate this memory whenever not required so that it can be used for some other purpose.
Properties of Dynamic Memory allocation
Memory is allocated at runtime.
Memory can be allocated and released at any time.
Heap memory is used here.
Dynamic memory allocation is slow.
It is more efficient as compared to Static memory allocation
The allocation process is simple is complicated.
Memory can be resized dynamically or reused.
The library functions of the stdlib.h header file, which helps to allocate memory dynamically are.
malloc()
calloc()
realloc()
free()
malloc()
This function is used to allocate the single block of requested memory. On Success, the malloc() function returns a pointer to the first byte of the allocated memory. The malloc() function gives a NULL output when the memory is not enough.
Syntax:
ptr=(data_type *)malloc(specified_size)
Here in the above syntax, ptr is a pointer of type data_type, and specified_size is the size in bytes required to allocate. The cast (data_type *) typecast the pointer returned by the malloc() function.
Example
ptr = (int*)malloc(100);
In the above example, the statement allocates 100 bytes of memory space, and the pointer variable ptr stores the address of the first byte in the memory.
calloc()
The calloc() function is used to allocate the memory as a number of elements of a given size. The calloc() function is similar to malloc() function. The only difference is that it takes two argument values. The first argument specifies the number of data items for which space is required, and the second argument specifies the size of each data item.
Syntax:
ptr=(data_type *)calloc(n, specified_size)
Here in the above syntax, ptr is a pointer of type data_type, n is the number of data items, and specified_size is the size in bytes required to allocate. The cast (data_type *) typecast the pointer returned by the malloc() function.
Example
ptr = (int*)calloc(4, sizeof (int));
In the above example, the statement allocates four blocks of memory, each block contains 4 bytes, and the starting address is stored in the pointer variable ptr.
realloc()
The realloc() function is used to increase the memory allocated by malloc() or calloc() function. This function alters the size of the memory block without losing the old data. The realloc() function takes two argument values. The first argument is a pointer variable to the block of memory that was previously allocated by calloc() or malloc(), and the second argument is the new size for that memory block.
Syntax:
realloc(pointer_variable, n);
Here in the above syntax, pointer_variable is a pointer to the block of memory that was previously allocated by malloc() or calloc() function, and n is the new size for that memory block.
Example
int *ptr;
ptr = (int*)malloc(100);
ptr = (int*)realloc(ptr, 400);
Here in the above example, we increased memory size from 100 bytes to 400 bytes.
free()
When memory is allocated dynamically by the malloc() and calloc() function, it should always be released when it is no longer required. Otherwise, it will consume memory until the program exit. The free() function is used to release this allocated memory space.
Syntax:
free(pointer_variable);
Here in the above syntax, the memory block pointed by the pointer 'pointer_variable' would be released back to the system.
C program for Dynamic Memory allocation
Here is a C program to illustrate the Dynamic Memory Allocation.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr1, *ptr2;
int size=10;
printf("The size of given array is: %d\n", size);
ptr1=(int*)malloc(size*sizeof(int));
ptr2=(int*)calloc(size,sizeof(int));
ptr1=(int*)realloc(ptr1, 20);
if (ptr1==NULL || ptr2==NULL)
{
printf("Memory allocation not sucessful.\n");
exit(0);
}
else
{
printf("Memory allocation using malloc() and calloc() is successful.\n");
ptr1=(int*)realloc(ptr1, 20);
if (ptr1==NULL)
{
printf("Memory allocation not sucessful.\n");
exit(0);
}
else
{
printf("Memory reallocation using realloc() is successful.\n");
}
}
free(ptr1);
printf("Memory became free.\n");
free(ptr2);
printf("Memory became free.\n");
return 0;
}
The size of given array is: 10
Memory allocation using malloc() and calloc() is successful.
Memory reallocation using realloc() is successful.
Memory became free.
Memory became free.
Here we see how malloc(), calloc, realloc(), and free() functions work in a program.
Dynamic memory allocation in an operating system refers to the process of allocating memory at runtime for processes and programs. It involves managing memory segments efficiently, ensuring proper allocation and deallocation to prevent memory leaks and optimize system performance.
What is the difference between static and heap memory allocation?
Static memory allocation assigns memory addresses to variables during compile time, while heap memory allocation dynamically assigns memory addresses during runtime using functions like malloc() and calloc(). Static allocation's memory size is fixed, determined by the compiler, whereas heap allocation's size can vary based on program needs.
What is the difference between malloc and calloc?
malloc(): Allocates a single block of memory without initializing it.
calloc(): Allocates multiple blocks of memory and initializes all bytes to zero.
Both are used for dynamic memory allocation but differ in initialization and block allocation.
Conclusion
In this article, we have extensively discussed Static and Dynamic Memory allocation and see the Dynamic Memory allocation in your C program using standard library functions like malloc(), calloc(), free(), and realloc(). We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our article on Dynamic memory and visit our library of curated blogs by clicking here. Do upvote our blog to help other ninjas grow. Happy Coding!