Dynamic Memory Allocation in C
The process of allocation of memory at the run time is known as dynamic memory allocation. In C it is done using four memory management functions that are given below. In this tutorial we will discuss them one by one in detail.
1. malloc()
2. calloc()
3. free()
4. realloc()
Note: The header file used for these four functions is stdlib.h.
In below image you can see that there is a free space between local variables and global variables region. This free space is known as heap and it is used for dynamic memory allocation.
Storage of C Program – Image Source |
malloc() in C
A block of memory can be allocated using malloc() function. Each allocated byte contains garbage value. Check its syntax below.
Syntax
pointer_name = (caste_type *) malloc(size_in_bytes);
Example
p = (int *) malloc (100 * sizeof (int) );
Above statement will allocate a memory space of 100 times the size of int data type. It means if integer takes 2 bytes then 200 bytes will be allocated. Here sizeof() is an operator that will give the size of int data type. The malloc() function will return the address of first byte of memory allocated and this address will be stored in pointer p.
Lets take one more example.
p = (char *) malloc(10);
This will allocate 10 bytes of memory which can store only character type values.
malloc() allocates block of contiguous bytes. If it is unable to find the specified size space in the heap then it will return NULL. It is good to check the pointer variable to ensure that the memory allocation was successful.
calloc() in C
It is used for allocating multiple blocks of memory of same size. Each byte is automatically set to zero. This function is normally used to allocate memory for derived data types like arrays and structures.
Syntax
pointer_name = (cast_type *) calloc(n, size_in-bytes);
Example
. . . . . struct student { char name[20]; int roll_no; }*p; p = (struct student*) calloc(5, sizeof(struct student) ); . . . . .
This will allocate 5 blocks of memory of same size that can store five records of structure student. If calloc() can’t find the specified memory space then it will return NULL.
free() in C
When we allocate memory dynamically then it’s our responsibility to free the memory when it is no longer in use, so that it can be used to store some other information. In C it can be done using free() function.
Syntax
free(pointer_name);
Example
free(p);
Here p is the pointer variable that contains the address of first byte of the memory block allocated by either malloc() or calloc() function.
realloc() in C
To change the size of the memory allocated dynamically we use realloc() function. We may face a condition that the previously allocated memory is not sufficient to store the data and more space is required. Or we have allocated a memory space larger than our need. In these cases we can easily alter the size of allocated memory using realloc().
Syntax
pointer_name = realloc(pointer_name, new_size);
Example
p = malloc(10);
p = realloc(p,20);
realloc() allocates new memory block and copy the data present in previous allocated block to new block. The previously allocated memory block is automatically freed. It also return NULL if can’t find the specified memory space in heap.
Lets make one program to understand the use of all these memory management functions in C.
#include<stdio.h> #include<stdlib.h> void main() { int *p,i; p=(int*)malloc(3*sizeof(int)); //checking the memory allocation was successful if(p==NULL) { printf("nInsufficient memory"); exit(0); } printf("Enter three numbers:"); for(i=0;i<3;++i) scanf("%d",p+i); for(i=0;i<3;++i) printf("%d ",*(p+i)); //altering the memory p=realloc(p,5*sizeof(int)); //checking the memory allocation was successful if(p==NULL) { printf("nInsufficient memory"); exit(0); } printf("nnEnter two more numbers:"); scanf("%d%d",p+3,p+4); for(i=0;i<5;++i) printf("%d ",*(p+i)); //free the memory free(p); }
Output
Explanation
1. In the above program I am first allocating memory space to store three integer values using malloc() function. You can clearly see that I am checking the pointer p before using to ensure that the memory allocation was successful.
2. After that I have taken three integer values and then stored them in memory and displayed them.
3. Now I am altering the size of memory that I have previously allocated. I am changing the memory size using realloc() function so that I can store two more integer values.
4. After reading two more values I am displaying all the values again. You can see in the output, the three values that I stored earlier remain unchanged.
5. At last I have freed the memory using free() function.
So this is all about dynamic memory allocation in C. If you find anything missing or incorrect in above tutorial then please inform me. Feel free to ask if you have any doubts regarding above tutorial.
What is int*
Is it integer pointer
Yes