Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
As can be seen, the length (size) of the array above is 9. But what if there
is a requirement to change this length (size)? For example,
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 1/20
12/13/24, 6:00 AM Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
1. malloc()
2. calloc()
3. free()
4. realloc()
C malloc()
Courses @35% Off C method
C Basics C Data Types C Operators C Input and Output C Control Flow C Fun
The “malloc” or “memory allocation” method in C is used to dynamically
allocate a single large block of memory with the specified size. It returns
a pointer of type void which can be cast into a pointer of any form. It
doesn’t Initialize memory at execution time so that it has initialized each
block with the default garbage value initially.
Syntax of malloc() in C
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 2/20
12/13/24, 6:00 AM Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
Example of malloc() in C
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6
7 // This pointer will hold the
8 // base address of the block created
9 int* ptr;
10 int n, i;
11
12 // Get the number of elements for the array
13 printf("Enter number of elements:");
14 scanf("%d",&n);
15 printf("Entered number of elements: %d\n", n);
16
17 // Dynamically allocate memory using malloc()
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 3/20
12/13/24, 6:00 AM Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
Output
C calloc() method
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 4/20
12/13/24, 6:00 AM Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
Syntax of calloc() in C
For Example:
Example of calloc() in C
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 5/20
12/13/24, 6:00 AM Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6
7 // This pointer will hold the
8 // base address of the block created
9 int* ptr;
10 int n, i;
11
12 // Get the number of elements for the array
13 n = 5;
14 printf("Enter number of elements: %d\n", n);
15
16 // Dynamically allocate memory using calloc()
17 ptr = (int*)calloc(n, sizeof(int));
18
19 // Check if the memory has been successfully
20 // allocated by calloc or not
21 if (ptr == NULL) {
22 printf("Memory not allocated.\n");
23 exit(0);
24 }
25 else {
26
27 // Memory has been successfully allocated
28 printf("Memory successfully allocated using
calloc.\n");
29
30 // Get the elements of the array
31 for (i = 0; i < n; ++i) {
32 ptr[i] = i + 1;
33 }
34
35 // Print the elements of the array
36 printf("The elements of the array are: ");
37 for (i = 0; i < n; ++i) {
38 printf("%d, ", ptr[i]);
39 }
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 6/20
12/13/24, 6:00 AM Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
}
41
42 return 0;
43 }
Output
C free() method
“free” method in C is used to dynamically de-allocate the memory. The
memory allocated using functions malloc() and calloc() is not de-
allocated on their own. Hence the free() method is used, whenever the
dynamic memory allocation takes place. It helps to reduce wastage of
memory by freeing it.
Syntax of free() in C
free(ptr);
Example of free() in C
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 7/20
12/13/24, 6:00 AM Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6
7 // This pointer will hold the
8 // base address of the block created
9 int *ptr, *ptr1;
10 int n, i;
11
12 // Get the number of elements for the array
13 n = 5;
14 printf("Enter number of elements: %d\n", n);
15
16 // Dynamically allocate memory using malloc()
17 ptr = (int*)malloc(n * sizeof(int));
18
19 // Dynamically allocate memory using calloc()
20 ptr1 = (int*)calloc(n, sizeof(int));
21
22 // Check if the memory has been successfully
23 // allocated by malloc or not
24 if (ptr == NULL || ptr1 == NULL) {
25 printf("Memory not allocated.\n");
26 exit(0);
27 }
28 else {
29
30 // Memory has been successfully allocated
31 printf("Memory successfully allocated using
malloc.\n");
32
33 // Free the memory
34 free(ptr);
35 printf("Malloc Memory successfully
freed.\n");
36
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 8/20
12/13/24, 6:00 AM Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
Output
C realloc() method
“realloc” or “re-allocation” method in C is used to dynamically change
the memory allocation of a previously allocated memory. In other words,
if the memory previously allocated with the help of malloc or calloc is
insufficient, realloc can be used to dynamically re-allocate memory. re-
allocation of memory maintains the already present value and new
blocks will be initialized with the default garbage value.
Syntax of realloc() in C
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 9/20
12/13/24, 6:00 AM Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
Example of realloc() in C
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 int main()
5 {
6
7 // This pointer will hold the
8 // base address of the block created
9 int* ptr;
10 int n, i;
11
12 // Get the number of elements for the array
13 n = 5;
14 printf("Enter number of elements: %d\n", n);
15
16 // Dynamically allocate memory using calloc()
17 ptr = (int*)calloc(n, sizeof(int));
18
19 // Check if the memory has been successfully
20 // allocated by malloc or not
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 10/20
12/13/24, 6:00 AM Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
if (ptr == NULL) {
22 printf("Memory not allocated.\n");
23 exit(0);
24 }
25 else {
26
27 // Memory has been successfully allocated
28 printf("Memory successfully allocated using
calloc.\n");
29
30 // Get the elements of the array
31 for (i = 0; i < n; ++i) {
32 ptr[i] = i + 1;
33 }
34
35 // Print the elements of the array
36 printf("The elements of the array are: ");
37 for (i = 0; i < n; ++i) {
38 printf("%d, ", ptr[i]);
39 }
40
41 // Get the new size for the array
42 n = 10;
43 printf("\n\nEnter the new size of the array:
%d\n", n);
44
45 // Dynamically re-allocate memory using
realloc()
46 ptr = (int*)realloc(ptr, n * sizeof(int));
47
48 if (ptr == NULL) {
49 printf("Reallocation Failed\n");
50 exit(0);
51 }
52
53 // Memory has been successfully allocated
54 printf("Memory successfully re-allocated
using realloc.\n");
55
56 // Get the new elements of the array
57 for (i = 5; i < n; ++i) {
58 ptr[i] = i + 1;
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 11/20
12/13/24, 6:00 AM Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
}
60
61 // Print the elements of the array
62 printf("The elements of the array are: ");
63 for (i = 0; i < n; ++i) {
64 printf("%d, ", ptr[i]);
65 }
66
67 free(ptr);
68 }
69
70 return 0;
71 }
Output
1 #include <stdio.h>
2 #include <stdlib.h>
3 int main()
4 {
5 int index = 0, i = 0, n,
6 *marks; // this marks pointer hold the base addre
7 // of the block created
8 int ans;
9 marks = (int*)malloc(sizeof(
10 int)); // dynamically allocate memory using mallo
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 12/20
12/13/24, 6:00 AM Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 13/20
12/13/24, 6:00 AM Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() - GeeksforGeeks
}
52 } while (ans == 1);
53 // print the marks of the students
54 for (i = 0; i <= index; i++) {
55 printf("marks of students %d are: %d\n ", i,
56 marks[i]);
57 }
58 free(marks);
59 }
60 return 0;
61 }
Output
Subscribe for 1 Year and get 1 Extra year of access completely FREE!
Upgrade to GeeksforGeeks Premium today!
Choose GeeksforGeeks Premium and also get access to 50+ Courses with
Certifications, Unlimited Article Summarization, 100% Ad free
environment, A.I. Bot support in all coding problems, and much more. Go
Premium!
https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/ 14/20