In this post, I will tell you how to write dynamic memory allocation in C. I am going to cover calloc, malloc, realloc and free.
Intro of Dynamic allocation
Dynamic allocation is the process by which memory is allocated as needed during runtime. There are many advantages to dynamic allocation but the most important one is that we can grow or shrink data as needed in our program.
Dynamic allocation functions
Now, we are going to talk over the dynamic allocation functions in C.
- Calloc:Allocates space for data(mostly for array) and initialize them as zero(default value) and returns a pointer. Its prototype is return-type var-pointer=(return-type-pointer) calloc(n-size,sizeof(return-type));
- Malloc:Allocates the request size of bytes and returns a pointer to the start of the allocated piece of memory.Its prototype is *return-type var-pointer=(return-type-pointer) malloc(n multiply with sizeof(return-type));
- realloc:Modified the size of previously allocated memory. Its prototype is return-type var-pointer=realloc(previouse-dynamic-allocation-pointer-var,nsizeof(return-type));
- free:releases previously allocated memory. Its prototype=*free(previous-pointer-var) Here the most used ones are Malloc and free.Note: If you are going to return dynamic allocation from a function make sure that the return-type
How to implement dynamic-allocation-functions in code
Note header file stdlib.h is needed for each of these functions
Now, we are going to see how to implement these functions in code. Let's see malloc:
#include<stdio.h>
//header file to use dynamic allocation function
#include<stdlib.h>
int main()
{
// declare a pointer
int *ptr;
//check the prototype,
// return-type var-pointer=ptr
//return-type-pointer=int *
//n=6; set the size any way you want
//sizeof(return-type)=int
ptr=(int *)malloc(6*sizeof(int));//(int *)=is also can be said typecast
//enter data
for(int i=0;i<6;i++){
printf("Enter the value of %d element: ",i+1);
scanf("%d",&ptr[i]);
}
printf("\n");
//check the values
for(int i=0;i<6;i++){
printf("The value of %d element is :%d\n",i+1,ptr[i]);
}
//if in case memory allocation fails then ptr will return NULL
return 0;
}
Now calloc:
#include<stdio.h>
//header file to use dynamic allocation function
#include<stdlib.h>
int main()
{
// declare a pointer
int *ptr;
//check the prototype,
// return-type var-pointer=ptr
//return-type-pointer=int *
//n=6; set the size any way you want
//sizeof(return-type)=int
ptr=(int *) calloc(6,sizeof(int));
//calloc intitalize and it's default value will be 0
for(int i=0;i<6;i++){
printf("The value of %d element is %d\n",i,ptr[i]);
}
//if in case memory allocation fails then ptr will return NULL
return 0;
}
now realloc:
#include<stdio.h>
//header file to use dynamic allocation function
#include<stdlib.h>
int main()
{
int *ptr;
ptr=(int *)malloc(6*sizeof(int));//(int *)=typecast
for(int i=0;i<6;i++){
printf("Enter the value of %d element: ",i+1);
scanf("%d",&ptr[i]);
}
printf("\n");
for(int i=0;i<6;i++){
printf("The value of %d element is :%d\n",i+1,ptr[i]);
}
//Reallocate ptr using realloc() and set value from 6 to 10
// return-type var-pointer=ptr
//previouse-dynamic-allocation-pointer-var=ptr,
//n=10; set the size any way you want
//sizeof(return-type)=int
ptr=realloc(ptr,10*sizeof(int));//recycle previous memory with new memory
for(int i=0;i<10;i++){
printf("Enter the value of %d element: ",i+1);
scanf("%d",&ptr[i]);
}
printf("\n");
for(int i=0;i<10;i++){
printf("The value of %d element is :%d\n",i+1,ptr[i]);
}
return 0;
}
And finally free:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr;
int n;
printf("How many integers you want to enter:");
scanf("%d",&n);
ptr=(int *) malloc(n*sizeof(int));
for(int i=0;i<n;i++){
printf("Enter value for %d element:",i);
scanf("%d",&ptr[i]);
}
printf("\n");
for(int i=0;i<n;i++){
printf("Value for %d element is %d\n",i,ptr[i]);
}
free(ptr);//free the memory which is being allocated used by calloc/malloc
return 655;
}
I hope this blog covers everything you need to know about dynamic memory allocation in C and how to implement them.
Feel free to comment here if you think anything needs to be added.