Andrea Ideas Back to catalog
C Program to Reverse an Array
Aug 28, 2026 Andrea

Mastering Array Resizing in C++

When dealing with dynamically sized data in C++, it's common to encounter the need to resize an array. This process, often required to accommodate or reduce dat...

When dealing with dynamically sized data in C++, it's common to encounter the need to resize an array. This process, often required to accommodate or reduce data volume, involves adjusting the size of an array and potentially handling its new and old elements. Here, we delve into methods for resizing arrays in C++.Let's kick off by exploring how to resize a raw, dynamically allocated array.

C Program to Reverse an Array

Raw pointers in C++ provide a flexible way to work with dynamically allocated memory. However, resizing a raw array involves several steps, including allocating new memory, copying data, and releasing the old memory. The C++ Standard Library offers more intuitive and safer alternatives through smart pointers and containers like vectors.

How to Print 2D Array in C

Resizing with std::vector

The std::vector container in C++ offers dynamic resizing, which simplifies our work significantly. Vectors automatically handle memory allocation and deallocation, and resizing can be as simple as pushing new elements onto the vector or setting its size.

C Arrays Cheat Sheet (1D, 2D Arrays, Syntax, Examples & Programs)

Here's how you can resize a vector using the resize function or by simply adding new elements. Let's say we start with a vector of size 3:

std::vector v = {1, 2, 3}; v.resize(5); // Resizes the vector to size 5, filling new elements with a default value (0 in this case) v.push_back(4); // Equivalent to resizing to size 5 and adding an element

Preserving old elements

What are Arrays in Java?

When resizing a vector, you might want to preserve the old elements and discarding only the new ones. You can use the resize function with a specified number of new elements:

std::vector v = {1, 2, 3}; v.resize(3, 0); // Sets the size to 3 and fills new elements with 0

Resizing with_custom elements

The resize function can also take a single element that will be used to fill any new spots:

What is a Multidimensional array in C?

std::vector v = {1, 2, 3}; v.resize(5, -1); // Sets the size to 5 and fills new elements with -1

Resizing and swapping with allocator

For a more explicit control over memory allocation, you can use std::allocator to handle memory and vectors to work with the data. The data and size functions let you access the underlying array and its size, respectively, enabling you to manually resize and swap the memory:

std::vector v; v.push_back(1); v.push_back(2); v.push_back(3); int* p = v.data(); std::allocator a; int* new_p = a.allocate(5 * sizeof(int)); a.construct(new_p, v[0]); a.construct(new_p + 1, v[1]); a.construct(new_p + 2, v[2]); a.construct(new_p + 3, int()); a.construct(new_p + 4, int()); v.swap(new_p); // `v` now has size 5, with uninitialized elements at the end

Be cautious when manually managing memory, as it's easy to introduce memory leaks or other issues. Generally, prefer using the safer and more expressive std::vector methods for resizing arrays in C++.

Batch Resize Images Online – Resize Multiple Images at Once
C Arrays (With Examples)
Array vs Pointer in C 😳 (Simple Beginner Guide)
a piece of paper that has some writing on it with numbers and symbols in the middle
Learn Arrays in C++ in 10 minutes.
Vector vs Array in C++ | STL Vector vs Array Differences Explained
What is 2D Array In C#
Arrays in C - GeeksforGeeks
Template Array Size
NumPy array reshaping guide

More shots

C Program to Reverse an Array
C Program to Reverse an Array
How to Print 2D Array in C
How to Print 2D Array in C
C Arrays Cheat Sheet (1D, 2D Arrays, Syntax, Examples & Programs)
C Arrays Cheat Sheet (1D, 2D Arrays, Syntax, Examples & Programs)
What are Arrays in Java?
What are Arrays in Java?
What is a Multidimensional array in C?
What is a Multidimensional array in C?
Batch Resize Images Online – Resize Multiple Images at Once
Batch Resize Images Online – Resize Multiple Images at Once
C Arrays (With Examples)
C Arrays (With Examples)
Array vs Pointer in C 😳 (Simple Beginner Guide)
Array vs Pointer in C 😳 (Simple Beginner Guide)

Related topics