Array of pointers & Dynamic memory allocation in c
Array of pointers & Dynamic memory allocation in c
An array of pointers in C is a collection of memory addresses, where each element of the array
is a pointer to a specific data type. This allows for flexible memory management and is often
used when working with strings, dynamic memory, or function pointers.
data_type * array_name[size];
Example
#include <stdio.h>
int main() {
arr[0] = &a;
arr[1] = &b;
arr[2] = &c;
return 0;
}
Dynamic Memory Allocation in C using malloc(), calloc(),
free() and realloc()
Dynamic memory allocation in C refers to the process of allocating memory during runtime, as opposed
to static memory allocation which is determined during compile time
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,
If there is a situation where only 5 elements are needed to be entered in
this array. In this case, the remaining 4 indices are just wasting memory in
this array. So there is a requirement to lessen the length (size) of the
array from 9 to 5.
Take another situation. In this, there is an array of 9 elements with all 9
indices filled. But there is a need to enter 3 more elements in this array. In
this case, 3 indices more are required. So the length (size) of the array
needs to be changed from 9 to 12.
They are:
malloc: Allocates a block of memory of a specified size. Contents are uninitialized.
calloc: Allocates memory for an array of elements and initializes them to zero.
realloc: Resizes a previously allocated memory block.
free: Frees the allocated memory.
malloc() method
Allocates a specified number of bytes and returns a pointer to the first byte of the allocated memory.
Syntax of malloc() in C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i;
int *arr;
printf("Enter the number of elements: ");
scanf("%d", &n);
// Allocating memory
arr = (int*)malloc(n * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed!\n");
return 1;
}
// Input elements
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Output elements
printf("The integers are:\n");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
// Freeing memory
free(arr);
return 0;
}
Realloc()
The realloc function in C is used to resize a previously allocated memory block dynamically. It
can increase or decrease the size of the memory block. If the new size is larger, additional memory is
uninitialized. If smaller, the excess memory is deallocated.
Syntax of realloc
void *realloc(void *ptr, size_t new_size);
ptr: Pointer to the memory block previously allocated (using malloc or calloc).
new_size: New size of the memory block (in bytes).
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n, new_n, i;
if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
if (arr == NULL) {
printf("Memory reallocation failed!\n");
return 1;
}
return 0;
}
Output
Case 1:
Enter the initial number of elements: 3
Enter 3 integers:
10 20 30
Enter the new number of elements: 5
Enter 2 more integers:
40 50
The array elements are:
10 20 30 40 50
Case 2:
Enter the initial number of elements: 5
Enter 5 integers:
12345
Enter the new number of elements: 3
The array elements are:
123
Key Points
Free()
In C, free is used to deallocate memory that was previously allocated dynamically using
functions like malloc, calloc, or realloc
Syntax: