In this article you will learn about dynamic memory allocation in C language. Since C is an organized language, it has a few fixed guidelines for programming. One of them incorporates changing the size of an exhibit. A cluster is an assortment of things put away at adjoining memory areas.
As it very well may be seen that the length (size) of the exhibit above made is 9. However, consider the possibility that there is a necessity to change this length (size). For Example,
- On the off chance that there is a circumstance where simply 5 components are should have been entered in this cluster. For this situation, the excess 4 records are simply squandering memory in this cluster. So there is a prerequisite to diminish the length (size) of the exhibit from all day.
- Take another circumstance. In this, there is a variety of 9 components with every one of the 9 records filled.
- However, there is a need to enter 3 additional components in this exhibit. For this situation, 3 files more are required. So the length (size) of the exhibit should be changed from 9 to 12.
- This methodology is alluded to as Dynamic Memory Allocation in C.
- Subsequently, C Dynamic Memory Allocation can be characterized as a strategy wherein the size of an information structure (like Array) is changed during the runtime.
- C gives a few capacities to accomplish these undertakings. There are 4 library capacities given by C characterized under <stdlib.h> header record to work with dynamic memory designation in C programming. They are:
Table of Contents
C malloc() technique
The “malloc” or “memory distribution” technique in C is utilized to progressively designate a solitary huge square of memory with the predefined size. It returns a pointer of type void which can be projected into a pointer of any structure. It doesn’t Initialize memory at execution time so it has introduced each square with the default trash esteem at first.
Syntax:
ptr = (cast-type*) malloc(byte-size) For Example:
ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory. And, the pointer ptr holds the address of the first byte in the allocated memory.
If space is insufficient, allocation fails and returns a NULL pointer.
Example:
- C
#include <stdio.h> #include <stdlib.h> int main() { // This pointer will hold the // base address of the block created int * ptr; int n, i; // Get the number of elements for the array printf ( "Enter number of elements:" ); scanf ( "%d" ,&n); printf ( "Entered number of elements: %d\n" , n); // Dynamically allocate memory using malloc() ptr = ( int *) malloc (n * sizeof ( int )); // Check if the memory has been successfully // allocated by malloc or not if (ptr == NULL) { printf ( "Memory not allocated.\n" ); exit (0); } else { // Memory has been successfully allocated printf ( "Memory successfully allocated using malloc.\n" ); // Get the elements of the array for (i = 0; i < n; ++i) { ptr[i] = i + 1; } // Print the elements of the array printf ( "The elements of the array are: " ); for (i = 0; i < n; ++i) { printf ( "%d, " , ptr[i]); } } return 0; } |
C calloc() technique
- It instates each square with a default esteem ‘0’.
- It has two boundaries or contentions as contrast with malloc().
Syntax:
ptr = (cast-type*)calloc(n, element-size); here, n is the no. of elements and element-size is the size of each element.
For Example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each with the size of the float.
Example:
- C
#include <stdio.h> #include <stdlib.h> int main() { // This pointer will hold the // base address of the block created int * ptr; int n, i; // Get the number of elements for the array n = 5; printf ( "Enter number of elements: %d\n" , n); // Dynamically allocate memory using calloc() ptr = ( int *) calloc (n, sizeof ( int )); // Check if the memory has been successfully // allocated by calloc or not if (ptr == NULL) { printf ( "Memory not allocated.\n" ); exit (0); } else { // Memory has been successfully allocated printf ( "Memory successfully allocated using calloc.\n" ); // Get the elements of the array for (i = 0; i < n; ++i) { ptr[i] = i + 1; } // Print the elements of the array printf ( "The elements of the array are: " ); for (i = 0; i < n; ++i) { printf ( "%d, " , ptr[i]); } } return 0; } |
Output:
Enter number of elements: 5 Memory successfully allocated using calloc. The elements of the array are: 1, 2, 3, 4, 5,
C realloc() technique
“realloc” or “redistribution” technique in C is utilized to powerfully change the memory designation of a formerly allotted memory. All in all, assuming the memory recently apportioned with the assistance of malloc or calloc is lacking, realloc can be utilized to powerfully redistribute memory. redistribution of memory keeps up with the all around present worth and new squares will be introduced with the default trash esteem.
Example:
- C
#include <stdio.h> #include <stdlib.h> int main() { // This pointer will hold the // base address of the block created int *ptr, *ptr1; int n, i; // Get the number of elements for the array n = 5; printf ( "Enter number of elements: %d\n" , n); // Dynamically allocate memory using malloc() ptr = ( int *) malloc (n * sizeof ( int )); // Dynamically allocate memory using calloc() ptr1 = ( int *) calloc (n, sizeof ( int )); // Check if the memory has been successfully // allocated by malloc or not if (ptr == NULL || ptr1 == NULL) { printf ( "Memory not allocated.\n" ); exit (0); } else { // Memory has been successfully allocated printf ( "Memory successfully allocated using malloc.\n" ); // Free the memory free (ptr); printf ( "Malloc Memory successfully freed.\n" ); // Memory has been successfully allocated printf ( "\nMemory successfully allocated using calloc.\n" ); // Free the memory free (ptr1); printf ( "Calloc Memory successfully freed.\n" ); } return 0; } |
Output:
Enter number of elements: 5 Memory successfully allocated using malloc. Malloc Memory successfully freed. Memory successfully allocated using calloc. Calloc Memory successfully freed.
C free() strategy
“free” technique in C is utilized to powerfully de-apportion the memory. The memory allotted utilizing capacities malloc() and calloc() isn’t de-designated all alone. Henceforth the free() strategy is utilized, at whatever point the unique memory designation happens. It assists with diminishing wastage of memory by liberating it.
Syntax:
ptr = realloc(ptr, newSize); where ptr is reallocated with new size 'newSize'.
If space is insufficient, allocation fails and returns a NULL pointer.
Example:
- C
#include <stdio.h> #include <stdlib.h> int main() { // This pointer will hold the // base address of the block created int * ptr; int n, i; // Get the number of elements for the array n = 5; printf ( "Enter number of elements: %d\n" , n); // Dynamically allocate memory using calloc() ptr = ( int *) calloc (n, sizeof ( int )); // Check if the memory has been successfully // allocated by malloc or not if (ptr == NULL) { printf ( "Memory not allocated.\n" ); exit (0); } else { // Memory has been successfully allocated printf ( "Memory successfully allocated using calloc.\n" ); // Get the elements of the array for (i = 0; i < n; ++i) { ptr[i] = i + 1; } // Print the elements of the array printf ( "The elements of the array are: " ); for (i = 0; i < n; ++i) { printf ( "%d, " , ptr[i]); } // Get the new size for the array n = 10; printf ( "\n\nEnter the new size of the array: %d\n" , n); // Dynamically re-allocate memory using realloc() ptr = realloc (ptr, n * sizeof ( int )); // Memory has been successfully allocated printf ( "Memory successfully re-allocated using realloc.\n" ); // Get the new elements of the array for (i = 5; i < n; ++i) { ptr[i] = i + 1; } // Print the elements of the array printf ( "The elements of the array are: " ); for (i = 0; i < n; ++i) { printf ( "%d, " , ptr[i]); } free (ptr); } return 0; } |
Output:
Enter number of elements: 5 Memory successfully allocated using calloc. The elements of the array are: 1, 2, 3, 4, 5, Enter the new size of the array: 10 Memory successfully re-allocated using realloc. The elements of the array are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
One another example for realloc() method is:
- C
#include <stdio.h> #include <stdlib.h> int main() { int index = 0, i = 0, n, *marks; // this marks pointer hold the base address // of the block created int ans; marks = ( int *) malloc ( sizeof ( int )); // dynamically allocate memory using malloc // check if the memory is successfully allocated by // malloc or not? if (marks == NULL) { printf ( "memory cannot be allocated" ); } else { // memory has successfully allocated printf ( "Memory has been successfully allocated by " "using malloc\n" ); printf ( "\n marks = %pc\n" , marks); // print the base or beginning // address of allocated memory do { printf ( "\n Enter Marks\n" ); scanf ( "%d" , &marks[index]); // Get the marks printf ( "would you like to add more(1/0): " ); scanf ( "%d" , &ans); if (ans == 1) { index++; marks = ( int *) realloc ( marks, (index + 1) * sizeof ( int )); // Dynamically reallocate // memory by using realloc // check if the memory is successfully // allocated by realloc or not? if (marks == NULL) { printf ( "memory cannot be allocated" ); } else { printf ( "Memory has been successfully " "reallocated using realloc:\n" ); printf ( "\n base address of marks are:%pc" , marks); ////print the base or ///beginning address of ///allocated memory } } } while (ans == 1); // print the marks of the students for (i = 0; i <= index; i++) { printf ( "marks of students %d are: %d\n " , i, marks[i]); } free (marks); } return 0; } |
Output:
Enter the new size of the array: 10 Memory successfully re-allocated using realloc. The elements of the array are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
Also Read: How to Enable and Disable Macros in Excel?