← Back to C++ Course | Chapter 14: Memory Management | Lesson 3 of 6

C++ Dynamic Arrays

A dynamic array is a row of boxes whose size you choose while the program runs, like ordering as many lockers as you need. You must free it when finished.
Syntax
cpp
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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Freeing a dynamic array with delete instead of delete[].
  2. Reading or writing outside the allocated range, such as index equal to the size.
  3. Forgetting to free the array, causing a leak.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.