Back to C Programming
2026-07-145 min read

C - Dynamic Array Resizing

Learn C - Dynamic Array Resizing step by step with clear examples and exercises.

Title: Dynamic Array Resizing in C - A full guide

Why This Matters

In C programming, dynamic array resizing is a crucial concept that allows us to adjust the size of an array during runtime, making it more flexible and efficient for handling varying data sizes. Understanding this topic can help you tackle real-world programming challenges, prepare for interviews, and debug common issues in your code.

Prerequisites

Before diving into dynamic array resizing, you should have a solid understanding of the following C concepts:

  1. Variables and data types
  2. Arrays
  3. Pointers
  4. Memory allocation using malloc() and free()
  5. Basic input/output operations with scanf() and printf()

Core Concept

Dynamic Array Allocation

Dynamic arrays are created using pointers, which allow us to allocate memory at runtime. To create a dynamic array, we first declare a pointer to an integer type:

int *arr;

Then, we use malloc() to allocate memory for the desired number of elements:

arr = (int*) malloc(size * sizeof(int));

Here, size is the initial size of the array. You can adjust this value based on your needs.

Resizing a Dynamic Array

Resizing a dynamic array involves reallocating memory when the current size is insufficient to hold additional elements. To resize an array, we use the realloc() function:

arr = (int*) realloc(arr, new_size * sizeof(int));

Here, new_size is the new size of the array after resizing. If the current memory block cannot be resized, realloc() will allocate a new memory block and copy the existing elements into the new one.

Worked Example

Let's create a simple program that dynamically allocates an array, adds elements to it, and resizes it when necessary:

#include <stdio.h>
#include <stdlib.h>

int main() {
int *arr; // Declare a pointer to an integer array
int size = 5; // Initial array size
int num_elements = 0; // Initialize the number of elements

arr = (int*) malloc(size * sizeof(int)); // Allocate memory for the initial array size

printf("Enter some integers (type -1 to stop):\n");

while (num_elements < size && scanf("%d", &arr[num_elements]) == 1) {
if (arr[num_elements] != -1) {
num_elements++; // Increment the number of elements

// Check if we need to resize the array
if (num_elements >= size) {
size *= 2; // Double the array size
arr = (int*) realloc(arr, size * sizeof(int)); // Reallocate memory for the new size
}
}
}

printf("The entered integers are:\n");
for (int i = 0; i < num_elements; i++) {
printf("%d ", arr[i]);
}

free(arr); // Free the allocated memory

return 0;
}

Common Mistakes

  1. Forgetting to allocate memory for the array: Always remember to use malloc() or another memory allocation function before accessing elements in a dynamic array.
  2. Not checking if malloc/realloc succeeded: It's essential to check if malloc() or realloc() successfully allocated memory by comparing the returned pointer to NULL.
  3. Accessing out-of-bounds elements: Make sure you don't access elements beyond the current size of the array, as this can lead to undefined behavior and potential crashes.
  4. Not freeing the memory: Always remember to use free() to release the memory allocated with malloc() or realloc(). Failing to do so can cause memory leaks.
  5. Incorrectly handling resizing: When resizing a dynamic array, ensure that you copy the existing elements into the new memory block and update the pointer accordingly.

Practice Questions

  1. Write a program that dynamically allocates an array of integers and asks the user to enter the values. The program should resize the array when it reaches its maximum size (5) and continue accepting input until the user enters a -1.
  2. Modify the previous program to dynamically determine the maximum size required based on the number of elements entered by the user before any resizing occurs.
  3. Write a function that takes an array of integers, its current size, and the new size as arguments, and resizes the array using realloc(). The function should return a pointer to the resized array or NULL if the resize failed.
  4. Implement a dynamic array implementation with the following additional features:
  • A function to add an element at the end of the array (similar to the example above)
  • A function to insert an element at a specific index in the array
  • A function to remove an element from the array by its index
  • A function to search for an element in the array

FAQ

  1. Why do we need dynamic arrays instead of fixed-size arrays? Dynamic arrays allow us to handle varying data sizes during runtime, making our programs more flexible and efficient. Fixed-size arrays have a predefined size, which can lead to inefficiencies when dealing with large or changing datasets.
  2. What happens if we try to reallocate memory for an array that is already of the maximum size allowed by the system? If you attempt to resize an array beyond the maximum allowable size, realloc() will return a NULL pointer, indicating that the operation failed. In this case, you should handle the error appropriately, such as by printing an error message or exiting the program gracefully.
  3. Is it necessary to free the memory allocated with malloc or realloc when we're done using the dynamic array? Yes, it is essential to call free() on the pointer returned by malloc() or realloc() once you no longer need the dynamically allocated memory. Failing to do so can lead to memory leaks and potential program crashes.
  4. What happens if we don't resize our dynamic array when it becomes full? If you don't resize your dynamic array when it becomes full, you may run out of memory and encounter errors or unexpected behavior in your program. It's essential to implement proper resizing strategies to ensure that your program can handle varying data sizes efficiently.
  5. Can we use realloc() to shrink the size of a dynamic array? Yes, realloc() can be used to shrink the size of a dynamic array by providing a smaller new size. However, Note that that if the current memory block cannot be resized, realloc() will allocate a new memory block and copy the existing elements into the new one, which may result in wasted memory if the new size is significantly smaller than the original size. In such cases, you should consider freeing the old memory and re-allocating a smaller memory block using malloc().