0% found this document useful (0 votes)
19 views

Array of pointers & Dynamic memory allocation in c

-
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Array of pointers & Dynamic memory allocation in c

-
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Array of pointers

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.

Declaring an Array of Pointers

data_type * array_name[size];

Example

#include <stdio.h>

int main() {

int a = 10, b = 20, c = 30;

int* arr[3]; // Array of 3 pointers to integers

// Assigning addresses of integers to pointers

arr[0] = &a;

arr[1] = &b;

arr[2] = &c;

// Accessing values using pointers

for (int i = 0; i < 3; i++) {

printf("Value at arr[%d]: %d\n", i, *arr[i]);

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.

This procedure is referred to as Dynamic Memory Allocation in C.

Dynamic memory allocation using malloc(), calloc(), free(),


and realloc() is essential for efficient memory management in C.

Therefore, C Dynamic Memory Allocation can be defined as a procedure in


which the size of a data structure (like Array) is changed during the runtime.

C provides some functions to achieve these tasks.


There are 4 library functions provided by C defined under <stdlib.h> header
file to facilitate dynamic memory allocation in C programming.

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

The “malloc” or “memory allocation” method in C is used to dynamically


allocate a single large block of memory with the specified size.

 Allocates a specified number of bytes and returns a pointer to the first byte of the allocated memory.

 if not sufficient space then The memory is not initialized.

Syntax of malloc() in C

ptr = (cast-type*) malloc(byte-size)


For Example:

#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;
}

Feature malloc calloc


Allocates and initializes a block of
Functionality Allocates a block of memory.
memory.
Memory is uninitialized; contains
Initialization Memory is initialized to zero.
garbage values.
Requires only the total size of Requires the number of elements and the
Arguments
memory to allocate. size of each element.
void *calloc(size_t num, size_t
Syntax void *malloc(size_t size);
size);
Usage ptr = malloc(5 * ptr = calloc(5, sizeof(int));
Feature malloc calloc
Example sizeof(int));
Slightly faster since it doesn’t
Speed Slightly slower due to zero-initialization.
initialize memory.

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;

// Step 1: Allocate initial memory


printf("Enter the initial number of elements: ");
scanf("%d", &n);
arr = (int *)malloc(n * sizeof(int));

if (arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}

// Step 2: Input values into the allocated memory


printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Step 3: Resize the memory using realloc


printf("Enter the new number of elements: ");
scanf("%d", &new_n);
arr = (int *)realloc(arr, new_n * sizeof(int));

if (arr == NULL) {
printf("Memory reallocation failed!\n");
return 1;
}

// If the size is increased, input additional values


if (new_n > n) {
printf("Enter %d more integers:\n", new_n - n);
for (i = n; i < new_n; i++) {
scanf("%d", &arr[i]);
}
}

// Step 4: Display all the elements


printf("The array elements are:\n");
for (i = 0; i < new_n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

// Free the allocated memory


free(arr);

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

1. If new_size is smaller, realloc frees the excess memory.


2. If new_size is larger, realloc attempts to expand the current block or allocates a new block,
copying the old data.
3. Always check if realloc returns NULL to handle reallocation failure.

Free()
In C, free is used to deallocate memory that was previously allocated dynamically using
functions like malloc, calloc, or realloc
Syntax:

void free(void *ptr);

You might also like