Whichever memory that we have used till now was static memory. We can not change that memory while executing that program. But in some applications, we do not know how much memory we need. Let’s take an example to understand this. An array arr stores the roll number of students in class.
Ex. int arr[50];
This array can store 50 students’ roll numbers. But every year, the number of students changes. Sometimes it can be more than 50 (we are lacking memory) or it can be less than 50 (there will be a waste of remaining memory of array arr). So in that case we use dynamic memory allocation.
Let’s start with dynamic memory allocation functions malloc(), calloc(), realloc() and free(). To use these dynamic allocation functions, we have to include a file by writing #include<stdlib.h>.
malloc()
To use the malloc function, we have to make a pointer. The data type of pointer and data type of value that we want to store must be the same. If this dynamic allocation is not possible then the pointer will get a NULL value. Below is the syntax for the same.
<pointer name> =(<data type> *)malloc(<total size of memory>);
This malloc will generate a whole block for memory storage. Let’s understand it by an example. Suppose we want to store int data having a total of 6 bytes of memory storage using malloc then below will be the code. And ptr is a pointer of int data type.
Ex. int *ptr; ptr = (int *)malloc(6);
For this, memory allocation will be as below diagram. Suppose memory starts from 1600.
It is recommended (not compulsory) to use the sizeof operator to store data of a particular data type in dynamic memory allocation. We can do it for same above example,
ptr = (int *)malloc(3*sizeof(int));
Because here, 3* sizeof(int) = 3 * 2 = 6 bytes
Example program structure to understand the use of malloc
In the below example, i is an int variable and ptr is a pointer of data type int. We have written malloc syntax to store 3 int values. Those values are taken from the user and those are printed.
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { int i; int *ptr; clrscr(); ptr = (int *)malloc(3*sizeof(int)); printf("enter 3 values \n"); for(i=0;i<3;i++) scanf("%d",ptr+i); printf("printing 3 values \n"); for(i=0;i<3;i++) printf("%d \n",*(ptr+i)); getch(); }
Output:
calloc()
To use calloc function also, we have to make a pointer. The data type of pointer and data type of value that we want to store must be the same. If this dynamic allocation is not possible then the pointer will get a NULL value. Below is the syntax for the same.
<pointer name> =(<data type> *)calloc(<number of blocks>,<size of each block>);
This calloc will generate the memory block having a particular number of blocks having a specific size as defined in syntax.
Let’s understand it by an example. Suppose we want to store int data having total 3 data(number of blocks=3) of int data type(size of each block= 2 bytes) using calloc then below will be the code. And ptr is a pointer of int data type.
Ex. int *ptr; ptr = (int *)calloc(3,2);
For this, memory allocation will be as below diagram. Suppose memory starts from 1600.
It is recommended (not compulsory) to use the sizeof operator to store data of a particular data type in dynamic memory allocation. We can do it for same above example,
ptr = (int *)calloc(3,sizeof(int));
Because here, sizeof(int) = 2 bytes
Example program structure to understand the use of calloc()
In the below example, i is an int variable and ptr is a pointer of data type int. We have written calloc syntax to store 3 int values. Those values are taken from the user and those are printed.
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { int i; int *ptr; clrscr(); ptr = (int *)calloc(3,sizeof(int)); printf("enter 3 values \n"); for(i=0;i<3;i++) scanf("%d",ptr+i); printf("printing 3 values \n"); for(i=0;i<3;i++) printf("%d \n",*(ptr+i)); getch(); }
Output:
realloc()
To use the realloc function also, we have to make a pointer. This function is used to change the size of memory allocated by malloc() and calloc() functions. The data type of pointer and data type of value that we want to store must be the same. If this dynamic allocation is not possible then the pointer will get a NULL value. Below is the syntax for the same.
<new pointer name> =(<data type> *)realloc(<old pointer name whose size we want to change>,<new size>);
realloc will change the size of the pointer generated by malloc() or realloc().
Let’s understand it by an example. Suppose I have assigned 6 bytes of memory using calloc function with pointer ptr1. And now we want to change its size to 8 bytes with a new pointer ptr2. Then below is the syntax,
int *ptr1; int *ptr2; ptr1=(int *)malloc(6); ptr2 = (int *)realloc(ptr1,8);
free()
This function is used to free memory which is allocated by malloc() or calloc() or realloc() function. Its main work is to deallocate dynamic memory. Below is the syntax for the same.
free(<pointer name>);
Example program structure to understand the use of realloc()
Let’s take the same example as the last program. i is an int variable and ptr is a pointer of data type int. We have written malloc syntax to store 3 int values. Those values are taken from the user and those are printed. After that, we are using the realloc function to increase the size of dynamic memory pointed by ptr. We are writing realloc syntax to store 4 int variables. And then we are taking 4 values from the user and printing them.
#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() { int i; int *ptr; clrscr(); ptr = (int *)malloc(3*sizeof(int)); printf("enter 3 values \n"); for(i=0;i<3;i++) scanf("%d",ptr+i); printf("printing 3 values \n"); for(i=0;i<3;i++) printf("%d \n",*(ptr+i)); printf("for realloc -----"); ptr = (int *)realloc(ptr,4*sizeof(int)); printf("enter 3 values \n"); for(i=0;i<4;i++) scanf("%d",ptr+i); printf("printing 3 values \n"); for(i=0;i<4;i++) printf("%d \n",*(ptr+i)); getch(); }
Output:
Learning from this blog:
- What is dynamic memory allocation?
- Difference between static and dynamic memory allocation.
- Syntax of malloc(), calloc(), free() and realloc() functions.
- Use of malloc(), calloc(), free() and realloc() functions.
- Write a program using dynamic memory allocation.