How to Dynamically Allocate an Array in C++? Last Updated : 21 May, 2024 Comments Improve Suggest changes Like Article Like Report In C++, dynamic memory allocation allows us to allocate memory during runtime. Dynamic allocation in an array is particularly useful when the size of an array is not known at compile time and needs to be specified during runtime. In this article, we will learn how to dynamically allocate an array in C++. Dynamic Allocation of Arrays in C++In C++, we use the new operator for dynamic memory allocation . To allocate an array dynamically, Start by declaring a pointer that will store the base address of the allocated array.Next, use the new operator to reserve memory space to accommodate an array of a particular data type.When making this allocation specify the size of the array that indicates how many elements it can contain. This specified size determines the amount of memory to be allocated.Syntax to Dynamically Allocate ArraysBelow is the general syntax for dynamically allocating an array in C++. data_type* pointer_variableName = new data_type[array_size];Here, data_type is the type of data that we want to store in the array. * pointer_variableName declares a pointer variable. new is a keyword used for dynamic memory allocation.array_size is the size of the array we want to allocate. C++ Program to Dynamically Allocate an ArrayThe below program demonstrates the dynamic array allocation in C++. C++ // C++ program to demonstrate how to dynamically allocate // array #include <iostream> using namespace std; int main() { // Take array size as input from user cout << "Enter the size of the array: "; int size; cin >> size; // Dynamically allocate an array int* arr = new int[size]; // Assign values to the array elements for (int i = 0; i < size; i++) { arr[i] = i + 1; } // Print the array elements cout << "Elements of the array are: "; for (int i = 0; i < size; i++) { cout << arr[i] << " "; } cout << endl; // Deallocate the memory delete[] arr; return 0; } Output Enter the size of the array: 5 Elements of the array are: 1 2 3 4 5 Time Complexity: O(1)Auxilliary Space: O(n), where n is the size of the dynamically allocated array. Note: Always remember to deallocate the memory using the delete[] operator after use to prevent memory leaks. Comment More infoAdvertise with us Next Article How to Dynamically Allocate an Array in C++? R rahul9yw89 Follow Improve Article Tags : C++ Programs C++ cpp-array C++-new and delete C++ Array Programs CPP Examples +2 More Practice Tags : CPP Similar Reads How to Dynamically Resize a C++ Array? In C++, an array is a collection of elements of the same type placed in contiguous memory locations. In this article, we will learn how to dynamically resize an array in C++. Example: Input: myArray = {10, 20, 30, 40, 50};Output:Resized array: 10 20 30 40 50 0 0 0 0 0 Resizing a Dynamic Array in C++ 3 min read How to Find the Size of a Dynamically Allocated Array in C++? In C++, dynamic memory allocation enables the users to manage the memory resources during the execution of the program and is very useful for arrays when the size of the array is not known at compile time or during any other times for flexibility. In this article, we will learn how to find the size 2 min read How to Declare an Array in C++? In C++, an array is a collection of similar data types in which elements are stored in contiguous memory locations. In this article, we will learn how to declare an array in C++. Declaring an Array in C++In C++, we can declare an array by specifying the type of its elements, followed by the name of 2 min read How to Empty an Array in C++? In C++, arrays are fixed-size containers that store elements of the same data type. Empty an array means removing all elements from it. In this article, we will see how to empty an array in C++. Example: Input: myArray = { 1, 8, 2, 9, 1, 5, 6 } Output: myArray = { 0, 0, 0, 0, 0, 0, 0 }Clear an Array 2 min read How to Initialize a Dynamic Array in C++? In C++, dynamic arrays allow users to allocate memory dynamically. They are useful when the size of the array is not known at compile time. In this article, we will look at how to initialize a dynamic array in C++. Initializing Dynamic Arrays in C++The dynamic arrays can be initialized at the time o 1 min read How to Deallocate a 2D Array in C++? In C++, an array of arrays is called a 2D array, or two-dimensional array. Deallocating a 2D array means freeing the allocated memory to prevent any memory leaks, especially when working with dynamically allocated memory. In this article, we will learn how to deallocate a 2D array in C++. Deallocati 3 min read How to Copy a Vector to an Array in C++? In C++, vectors are dynamic arrays that can grow and reduce in size as per requirements. Sometimes, we may need to copy the contents of a vector to the POD array. In this article, we will learn how to copy a vector to an array in C++. Example: Input: myVec = {10,20,30,40,50,60};Output: array: {10,20 2 min read How to Delete an Element from an Array in C++? In C++, arrays are data structures that allow users to store data of the same type in contiguous memory locations. In this article, we will learn how to delete an element from an array in C++. Example: Input:myarr = {20, 5, 1, 10, 15};Target: 1Output:Array after deletion: 20 5 10 15Remove an Element 3 min read How to pass and return a 3-Dimensional Array in C++? In C++ a 3-dimensional array can be implemented in two ways: Using array (static)Using vector (dynamic) Passing a static 3D array in a function: Using pointers while passing the array. Converting it to the equivalent pointer type. char ch[2][2][2];void display(char (*ch)[2][2]) { . . .} Program to p 3 min read How to Create Array of Arrays in C++ Arrays are basic C++ data structures that allow users to store the same data type in memory sequentially. To manage more complicated data structures, you may sometimes need to build an array of arrays, often called a 2D array or a matrix. In this article, we will learn how to create an array of arra 3 min read Like