C++ Dynamic Arrays
In this page:
data_type *array_name = new data_type[size];
array_name[index] = value;
delete[] array_name;
Allocating Dynamic Arrays
A dynamic array is allocated on the heap with new[] at runtime, which means its size can be a variable computed while the program is running, unlike a fixed-size stack array whose length must be a compile-time constant.
This makes it the right choice whenever you don't know how much data you'll need to store until later.
Example: Allocating Dynamic Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare size and set it to 5
int size = 5;
// Allocate an object on the heap with new
int *arr = new int[size];
// Print to the console with cout
std::cout << "Allocated " << size << " ints" << std::endl;
// Free the heap memory with delete
delete[] arr;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Initializing Dynamic Arrays
You can fill a dynamic array element-by-element in a loop after allocation, or use value initialization -- new int[n]() with the trailing parentheses -- to zero-initialize every element automatically instead of leaving them with unpredictable garbage values.
Example: Initializing Dynamic Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
int *arr = new int[3]();
// Print to the console with cout
std::cout << arr[0] << " " << arr[1] << " " << arr[2] << std::endl;
// Free the heap memory with delete
delete[] arr;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Accessing and Modifying
Once allocated, a dynamic array is accessed with the same arr[i] syntax as a stack array, since the pointer new[] returns behaves like the array's name.
Internally that pointer just stores the memory address of the first element, and indexing computes an offset from it.
Example: Accessing and Modifying
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
int *arr = new int[3];
arr[0] = 10;
arr[1] = 20;
// Print to the console with cout
std::cout << arr[0] << " " << arr[1] << std::endl;
// Free the heap memory with delete
delete[] arr;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Deallocating Dynamic Arrays
delete[] must be called exactly once on a dynamic array when you're done with it, or the heap memory it occupies is never returned to the system.
Setting the pointer to nullptr afterward is good practice so any accidental later use is caught rather than silently corrupting memory.
Example: Deallocating Dynamic Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
int *arr = new int[3]{1, 2, 3};
// Free the heap memory with delete
delete[] arr;
arr = nullptr;
// Print to the console with cout
std::cout << "Freed" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Resizing Dynamic Arrays
C++ arrays can't grow or shrink in place -- to "resize" one, you allocate a new, larger array, copy the existing elements into it, and delete the old array.
This copy-and-replace pattern is exactly what std::vector automates internally, which is why vector is usually preferred over manual dynamic arrays.
Example: Resizing Dynamic Arrays
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
int *arr = new int[2]{1, 2};
// Allocate an object on the heap with new
int *bigger = new int[4];
// Loop: repeat while the condition holds
for (int i = 0; i < 2; i++) bigger[i] = arr[i];
// Free the heap memory with delete
delete[] arr;
arr = bigger;
// Print to the console with cout
std::cout << arr[0] << " " << arr[1] << std::endl;
// Free the heap memory with delete
delete[] arr;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Freeing a dynamic array with
deleteinstead ofdelete[]. - Reading or writing outside the allocated range, such as index equal to the size.
- Forgetting to free the array, causing a leak.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: