C Language / Dynamic Memory Allocation
Dynamic memory allocation concept
It is used to allocate memory at runtime and it consists of 4 functions of stdlib.h header file. The functions were
1. malloc(). 2. calloc().
3. realloc() 4. free()
Difference between static and dynamic memory allocations.
Property
|
Static
Memory Allocation
|
Dynamic
Memory Allocation
|
memory allocation done at
|
Compile time.
|
Run time.
|
Memory Size while program execution
|
Can’t be increased.
|
Can be increased
|
Uses Concept
|
array
|
linked list
|
Dynamic Memory Allocations Functions List
S.No
|
Function
|
Details
|
1
|
malloc()
|
Allocates requested memory.
|
2
|
calloc()
|
Allocates requested memory for an array.
|
3
|
realloc()
|
Used to modify the size of previously allocated
memory space.
|
4
|
free()
|
Frees / empties / Clears dynamically allocated
memory.
|
Functions List with Syntax and Example
function
|
Syntax
|
Example
|
malloc()
|
ptr = (cast_type *) malloc (byte_size);
|
ptr = (int *) malloc (40)
|
calloc()
|
ptr = (cast_type *) calloc (n, size);
|
ptr = calloc(20, sizeof(int));
|
realloc()
|
ptr = realloc (ptr,newsize);
|
ptr = (char *) realloc(ptr, 40);
|
free()
|
free(ptr)
|
free(ptr)
|
Programfor reading and printing array using malloc, calloc function
|
Output
|
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &num);
ptr = (int*) malloc(num * sizeof(int));
printf("Enter array elements:\n ");
for(i = 0; i < num; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
}
|
Enter number of elements: 5
Enter array elements:
1
2
3
4
5
Sum = 15
|
#include <stdio.h>
#include <stdlib.h>
void main()
{
int *ptr;
int n, i;
n = 5;
printf("Enter number of elements: %d\n", n);
ptr = (int*) calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory allocation Failed.\n");
}
else {
printf("Memory allocated successfully using calloc \n");
// Get array elements
for (i = 0; i < n; i++)
ptr[i] = i + 1;
// Print array elements
printf("Array elements are: ");
for (i = 0; i < n; i++)
printf("%d\t", ptr[i]);
}
}
|
Enter number of elements: 5
Memory successfully allocated using malloc
Array elements are: 1 2
3
4
5
|
#include <stdio.h>
#include <stdlib.h>
void main()
{
int *ptr;
int n, i;
n = 5;
printf("Enter number of elements: %d\n", n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory allocation Failed.\n");
}
else {
printf("Memory allocated successfully using malloc\n");
// Get array elements
for (i = 0; i < n; i++)
ptr[i] = i + 1;
// Print array elements
printf("Array elements are: ");
for (i = 0; i < n; i++)
printf("%d\t", ptr[i]);
printf("\n");
n = 9;
printf("Enter number of elements: %d\n", n);
ptr = (int*)realloc(ptr, n *sizeof(int));
if (ptr == NULL) {
printf("Memory allocation Failed.\n");
}
else {
printf("Memory allocated successfully using malloc\n");
// Get array elements
for (i = 0; i < n; i++)
ptr[i] = i + 1;
// Print array elements
printf("Array elements are: ");
for (i = 0; i < n; i++)
printf("%d\t", ptr[i]);
}
|
Enter number of elements: 5
Memory allocated successfully using malloc
Array elements are: 1 2
3
4
5
Enter number of elements: 9
Memory allocated successfully using malloc
Array elements are: 1
2
3
4
5
6
7
8
|
|