
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Initialize a Dynamic Array in C++
Dynamic arrays are a type of array that can grow or shrink their size as elements are added or removed. They are created using pointers and memory management functions such as new and delete. In this article, we will learn different ways to initialize a dynamic arrays in C++.
Here is the list of approaches to initialize a dynamic array in C++:
Initializing Dynamic Arrays Using new and ()
This method is used to initialize all the elements of a dynamic array to zero. The syntax for this method is as follows:
int* arr = new int[5](); // All elements initialized to 0
In this syntax, new int[5]() allocates memory for an array of integers of size 5 and set all elements to zero. Let's see a working example:
Example
In the code below, we will create a dynamic array of integers with 5 elements, initialize them to zero, and then print the elements of the array.
#include <iostream> using namespace std; int main() { int* arr = new int[5](); // All elements initialized to 0 // Print the elements of the array for (int i = 0; i < 5; i++) { cout << arr[i] << " "; } cout << endl; // Deallocate the memory delete[] arr; return 0; }
The output of the above code will be:
0 0 0 0 0
Initializing Dynamic Arrays Using new and {}
In this method, we can initialize the elements of a dynamic array to any values, not just zeros. The list of values to be initialized is provided in curly braces after declaration. If the list is shorter than the array size, the remaining elements are Initialized with zero. The syntax for this method is as follows:
int* arr = new int[5]{1, 2, 3}; // Initializes first three elements to 1, 2, 3 and the rest to 0
In this syntax, new int[5]{1, 2, 3} allocates memory for an array of integers of size 5 and initializes the first three elements to 1, 2, and 3, and the remaining elements to zero. Let's see a working example.
Example
In the code below, we will create a dynamic array of integers with 5 elements, initialize the first three elements to 1, 2, and 3, and the rest to zero, and then print the elements of the array.
#include <iostream> using namespace std; int main() { int* arr = new int[5]{1, 2, 3}; // Print the elements of the array for (int i = 0; i < 5; i++) { cout << arr[i] << " "; } cout << endl; // Deallocate the memory delete[] arr; return 0; }
The output of the above code will be:
1 2 3 0 0
Initializing Dynamic Arrays Using Loop
In this method, we can run a loop to initialize the elements of a dynamic array to any specific values at any index or any pattern of values. The syntax for this method is as follows:
int* arr = new int[5]; // Allocate memory for 5 integers for (int i = 0; i < 5; i++) { arr[i] = i * 2; // Initialize each element to double its index }
In the above syntax, we initialize each element of the dynamic array to double its index. Let's see a working example.
Example
In the code below, we will create a dynamic array of integers with 5 elements, initialize each element to triple its index, and then print the elements of the array.
#include <iostream> using namespace std; int main() { int* arr = new int[5]; // Allocate memory for 5 integers // Initialize each element to triple its index for (int i = 0; i < 5; i++) { arr[i] = i * 3; } // Print the elements of the array for (int i = 0; i < 5; i++) { cout << arr[i] << " "; } cout << endl; // Deallocate the memory delete[] arr; return 0; }
The output of the above code will be:
0 3 6 9 12