How to Initialize a Dynamic Array in C++? Last Updated : 31 Jan, 2024 Comments Improve Suggest changes Like Article Like Report 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 of their declaration by using list initialization as shown: data_type* pointer_variableName = new data_type[ array_size] {element1, element2, ...} ;C++ Program to Initialize Dynamic Arrays C++ // C++ program to demonstrates the initialization of a // dynamic array using a new keyword. #include <iostream> using namespace std; int main() { int size = 5; // initializing a dynamic array int* arr = new int[size]{ 1, 2, 3, 4, 5 }; // printing the array elements cout << "Elements of the array are: " << endl; for (int i = 0; i < size; i++) { cout << arr[i] << " "; } // freeing-up memory space by deleting arr delete[] arr; return 0; } OutputElements of the array are: 1 2 3 4 5 Note: If we create a dynamic array without specifying initial values, the elements will not be initialized, and their values will be undefined. Comment More infoAdvertise with us Next Article How to Initialize a Dynamic Array in C++? V vivekchaudharyy Follow Improve Article Tags : C++ Programs C++ cpp-array Dynamic Memory Allocation C++-new and delete C++ Array Programs CPP Examples +3 More Practice Tags : CPP Similar Reads How to Initialize an Array in C++? In C++, an array is a collection of similar datatypes stored in contiguous memory locations in which each element can be accessed using their indices. In this article, we will learn how to initialize an array in C++. Initializing an Array in C++To initialize an array in C++, we can use the assignmen 2 min read 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 Initialize a Set with an Array in C++? In C++, an array stores data in contiguous memory locations and can have duplicates as well whereas a set stores unique elements only in a sorted order. In this article, we will learn how to initialize a set with an array in C++. For Example, Input:arr[]={2, 1, 5, 4, 3, 5};Output:Element in Set are: 2 min read How to Initialize Vector of Char Arrays in C++? In C++, a vector is a dynamic array that can grow and shrink in size. A char array is a sequence of characters, typically used to represent strings. In this article, we will learn how to initialize a vector of char arrays in C++. Example: myVector: {âappleâ, âbananaâ, âcherryâ}Initializing a Vector 2 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read How to Dynamically Allocate an Array in C++? 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 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 Dynamic initialization of object in C++ In this article, we will discuss the Dynamic initialization of objects using Dynamic Constructors. Dynamic initialization of object refers to initializing the objects at a run time i.e., the initial value of an object is provided during run time.It can be achieved by using constructors and by passin 4 min read How to Initialize 2D Vector in C++? Initializing a 2D vector refers to the process of assigning initial values to the elements of a 2D vector. In this article, we will learn different methods to initialize a 2D vector in C++.The simplest way to initialize a 2D vector is by passing the elements inside an initializer list. This method i 3 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 Like