English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn to use standard library functions: malloc(), calloc(), free(), and realloc() to dynamically allocate memory in C language programs.
As you know, an array is a collection of a fixed number of values. After declaring the size of the array, you will not be able to change it.
Sometimes, the size of the array you declare may be insufficient. To solve this problem, you can manually allocate memory at runtime. This is called dynamic memory allocation in C language programming.
Standard library functions such as malloc(), calloc(), realloc(), and free() can be used for dynamic memory allocation. These functions are defined in the <stdlib.h> header file.
The name 'malloc' indicates memory allocation.
The malloc() function reserves a memory block of a specified number of bytes. And, it returns a voidPointerIt can be converted to any form of pointer.
ptr = (castType*) malloc(size);
Example
ptr = (float*) malloc(100 * sizeof(float));
The above statement allocated400-byte memory. Because the size of a floating-point number is4Bytes. And, the pointer ptr saves the address of the first byte of the allocated memory.
If memory allocation fails, the expression will produce a NULL pointer.
The name 'calloc' indicates contiguous allocation.
The malloc() function allocates memory and retains uninitialized memory. The calloc() function allocates memory and initializes all bits to zero.
ptr = (castType*)calloc(n, size);
Example:
ptr = (float*)calloc(25, sizeof(float));
The above statement allocates continuous space in memory for25elements of float type are allocated continuous space.
Memory allocated using calloc() or malloc() does not release itself. It must be explicitly released using free().
free(ptr);
This statement releases the space allocated in the memory pointed to by ptr.
//Program to calculate the sum of n numbers entered by the user #include <stdio.h> #include <stdlib.h> int main() { int n, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); ptr = (int*)malloc(n * sizeof(int)); // If memory allocation fails if(ptr == NULL) { printf("Error! Memory not allocated."); exit(0); } printf("Enter element: "); for(i = 0; i < n; ++i) { scanf("%d", ptr + i); sum += *(ptr + i); } printf("Sum = %d", sum); //Release memory free(ptr); return 0; }
Here, we dynamically allocated memory for n integers.
//Program to calculate the sum of n numbers entered by the user #include <stdio.h> #include <stdlib.h> int main() { int n, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); ptr = (int*)calloc(n, sizeof(int)); if(ptr == NULL) { printf("Error! Memory not allocated."); exit(0); } printf("Enter element: "); for(i = 0; i < n; ++i) { scanf("%d", ptr + i); sum += *(ptr + i); } printf("Sum = %d", sum); free(ptr); return 0; }
If the dynamically allocated memory is insufficient or exceeds the required amount, the realloc() function can be used to change the size of the previously allocated memory.
ptr = realloc(ptr, x);
Here, ptr is reallocated to the new size x.
#include <stdio.h> #include <stdlib.h> int main() { int *ptr, i, n1, n2; printf("Enter size: "); scanf("%d", &n1); ptr = (int*)malloc(n1 * sizeof(int)); printf("Previous allocated memory address: "); for (i = 0; i < n1; ++i) printf("睵n", ptr + i); printf("\nEnter new size: "); scanf("%d", &n2); //Reallocate memory ptr = realloc(ptr, n2 * sizeof(int)); printf("Newly allocated memory address: "); for (i = 0; i < n2; ++i) printf("睵n", ptr + i); free(ptr); return 0; }
The output when running the program is:
Enter the size: 3 Address of previously allocated memory: 7452312 7452316 7452320 Enter the new size: 5 Address of newly allocated memory: 7452312 7452316 7452320 7452324 7452328