How to regrow and shrink a 1D array in C++

In C++, an array is a collection of elements of the same data type arranged in a contiguous memory block. A 1D array, also known as a one-dimensional array, is a linear structure that stores elements in a single row or line. The elements of a 1D array can be accessed using their indices.

Visual representation of 1D array
Visual representation of 1D array

What is a 1D dynamic array?

A dynamic array in C++ is an array whose size can be changed during runtime. It is created using dynamic memory allocation instead of being statically allocated like a regular array. In a dynamic array, memory is allocated from the heap at runtime using the new operator. Dynamic arrays are implemented using pointers.

Dynamic array to regrow and shrink in C++

In C++, the size of a regular (statically allocated) array is determined at compile time and cannot be changed once defined. This means that we cannot directly resize or regrow a regular array.

Dynamic arrays provide the flexibility to resize an array during runtime by allocating a new memory block with the desired size and copying the elements from the old array to the new one. By using dynamic memory allocation, we can allocate memory for an array based on user input, calculations, or other dynamic factors.

Here's an example of how we can regrow or shrink a 1D array dynamically in C++:

Code

Consider the following playground:

#include <iostream>
using namespace std;
int* resizeArray(int* array1d, int currentSize, int newSize) {
// Create a new dynamic array with the desired size
int* newArray = new int[newSize];
// Copy the elements from the original array to the new array
int elementsToCopy = (currentSize < newSize) ? currentSize : newSize;
for (int i = 0; i < elementsToCopy; i++) {
newArray[i] = array1d[i];
}
// Delete the original array
delete[] array1d;
return newArray;
}
void printArray(int* array1d, int size) {
for (int i = 0; i < size; i++) {
cout << array1d[i] << " ";
}
cout << endl;
}
int main() {
int size = 5;
int* array1d = new int[size];
// Populate the array
for (int i = 0; i < size; i++) {
array1d[i] = i;
}
// Resize the array to a larger size
int newSize = 10;
array1d = resizeArray(array1d, size, newSize);
size = newSize;
// Display the resized array
printArray(array1d, size);
// Shrink the array to a smaller size
newSize = 3;
array1d = resizeArray(array1d, size, newSize);
size = newSize;
// Display the shrunk array
printArray(array1d, size);
// Don't forget to free the memory
delete[] array1d;
return 0;
}

Explanation

Let's understand the above code line-by-line:

  • Lines 5–19: We define a function named resizeArray:

    • Line 5: The resizeArray function takes three parameters: array1d (a pointer to the original array), currentSize (the current size of the array), and newSize (the desired new size of the array). The function returns a pointer to the resized array.

    • Line 7: We dynamically allocate memory for a new array of integers using the new operator. The size of the new array is newSize.

    • Line 10: We determine the number of elements to copy from the original array to the new array. We take the minimum value between currentSize and newSize to ensure that no out-of-bounds access occurs during the copy.

    • Lines 11–12: We use a loop to copy the specified number of elements from the original array to the new array.

    • Line 16: We free the memory allocated for the original array using the delete[] operator.

    • Line 18: We return the pointer to the newly resized array.

  • Lines 21–26: We declare a function named printArray:

    • Line 21: The printArray function takes two parameters: array1d, which is a pointer to an integer array, and size, which is an integer representing the size of the array. The function has a return type of void, indicating that it does not return a value.

    • Lines 22–23: We use a loop to display the elements of the array using cout. The elements are separated by spaces.

    • Line 25: We print a new line at the end.

  • Lines 28–57: We define our main function:

    • Line 29: We declare a variable size and set its value to 5.

    • Line 30: We allocate memory for the original array array1d using the new operator.

    • Lines 33–34: We use a loop to populate the elements of the original array with consecutive values starting from 0.

    • Lines 38–40: We demonstrate how to resize the array.

      • Line 38: The newSize variable is set to 10.

      • Line 39: The resizeArray function is called to resize the array and the returned pointer is assigned back to array.

      • Line 40: The size variable is updated to reflect the new size.

    • Line 43: We display the elements of the resized array using the printArray function.

    • Lines 46–48: We demonstrate shrinking the array.

      • Line 46: The newSize variable is set to 3.

      • Line 47: The resizeArray function is called again to resize the array1d accordingly, and the returned pointer is assigned back to array1d.

      • Line 48: The size variable is updated.

    • Line 51: We display the elements of the shrunk array using the printArray function.

    • Line 54: We deallocate the memory allocated for the final array using the delete[] operator.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved