What is a Dynamic Array?
A dynamic array is identical to a standard array in appearance, but its size can be changed while the code is running. Dynamic Array components take up a contiguous memory block. After an array has been defined, it is not possible to modify its size. In contrast, a dynamic array is not like a static array. Even after it has been occupied, a dynamic array can extend its size. Elements can be added constantly at the dynamic array’s end position by utilizing the reserved space until it is entirely occupied.
Key Factors of Dynamic Array in C++:
The performance of the array is determined by its starting size and growth factor. Take note of the following points:
- If an array is modest in size and has a slower growth factor, it will continue to reallocate memory more often. As a result, the array’s performance will suffer.
- If an array is larger and has a high growth factor, it will have a large amount of unused memory. As a result, resizing processes may take longer. The array’s performance will also suffer as a result
Example 1:
The new keyword is used to build a dynamic array in the following C++ program. After that, the keyword returns a reference to the first item. The header section has the included iostream library file to use its methods. The namespace file is also included which allows utilizing its class without being called. Then the main function is invoked in which we have declared two variables “p” and “num” of integer data type.
In the next step, the cout statement prints the statement “Enter the numbers”. The cin command takes input from the user and allocates it to the variable “num”. The next step has the pointer variable “Array” which holds the integer values of the variable “num”. The user’s inputted number will be printed using the cout command. Then, we have for loop condition which iterates over each element entered by the user. The array as “Array” is declared in the cin command which reads the input entered by the user.
After the termination of the loop, the “your numbers are” statement will print on the console screen. Again, we have a for loop condition but this time this for loop condition iterates over an array of elements. Note that we have allowed a user to set array size. As a result, the array’s size is defined at runtime.
using namespace std;
int main() {
int p, num;
cout<< "Enter the numbers:" <>num;
int *Array = new int(num);
cout<< "Enter " << num << " numbers" <<endl;
for (p = 0; p > Array[p];
}
cout<< "Your numbers are: ";
for (p = 0; p < num; p++) {
cout<< Array[p] << " ";
}
cout<< "\n ";
return 0;
}
The user is prompted to input the number in the console prompt. After input, the number for array size displays the numbers of specified array size. The resultant array is shown on the console prompt of Ubuntu.
Example 2:
An initializer list can be used to set a dynamic array. Let’s illustrate this with an example to see how this works. First, we added the iostream file and the std namespace file in the header section. Following that, we invoked the main function. The logic of the program should be included in the function’s body. Then we have defined a variable as “a” of integer data type.
After declaring the integer variable, we have a dynamic array declaration as “Arr” that uses an initializer list. We have four integer entries in the array. The cout command will print the statement “Elements of array” before displaying the array elements.
In the next step, we have a for loop which iterates over elements present in a specified array. Through the cout command, the elements of the given array will be printed on the console prompt.
using namespace std;
int main(void) {
int a;
int *Arr{ new int[4]{ 9, 23, 1, 17 } };
cout<< "Elements of Array: " <<endl;
for (a = 0; a < 4; a++) {
cout<<Arr[a] <<endl;
}
return 0;
}
The following is the outcome we got from the above program execution:
Example 3:
Once the objective of the dynamic array has been achieved, it should be removed from the computer memory. The delete expression can be used to do thisso that the memory space is free and used to store additional data. We have to use delete[] to remove the dynamic array from the memory of the system. The square bracket [] with the keyword delete commands the CPU to remove many variables rather than just one.
Let’s begin the implementation of the program. We have imported the required file in the header section. Then, the main function is called. The integer variables “i” and “no” are declared in the main function. After defining these variables, we have the cout statement “Input Number” which is for the user to enter the number. We get a number from the user and save it in the variable “no” using the cin command.
Then, declare a pointer variable “MyArr” which stores the integers in the memory. The number entered by the user will be printed in the second cout command of this program. The for loop statement is utilized for the iteration over the user entered number. In the end, we have constructed the delete[] statement that erases the array given in the program and frees up space in the memory.
using namespace std;
int main() {
int i, no;
cout<< "Input Number:" <>no;
int *MyArr = new int(no);
cout<< "Input " << no << " numbers" <<endl;
for (i = 0; i>MyArr[i];
}
cout<< "Input numbers are: ";
for (i = 0; i< no; i++) {
cout<<MyArr[i] << " ";
}
cout<<endl;
delete [] MyArr;
return 0;
}
Upon execution of the program, we got the following output. When the program is terminated the array will be deleted.
Example 4:
We can define a pointer array of size “X” dynamically and then allocate memory of size “Y” dynamically for each row ass seen in the following example. At first, we have defined the matrix in the header section. In the next step, we have the main function where we have a pointer variable “arr”. The pointer variable contains the array of size “X”.
Now, the for loop statement allocates each row a memory size “Y”. Then, we have a nested loop for dynamically assigning values to a memory that has been allocated. The rand function will generate a random number for the 2D array. In the next nested loop, we have printed the 2D array through the std::cout statement. Upon the program termination, the specified 2D array will be erased from the allocated memory space as we have used delete[] in the end.
#define X 3
#define Y 4
int main()
{
int** arr = new int*[X];
for (int i = 0; i< X; i++) {
arr[i] = new int[Y];
}
for (int i = 0; i< X; i++)
{
for (int j = 0; j < Y; j++) {
arr[i][j] = rand() % 10;
}
}
for (int i = 0; i< X; i++)
{
for (int j = 0; j < Y; j++) {
std::cout<<arr[i][j] << " ";
}
std::cout<< std::endl;
}
for (int i = 0; i< X; i++) {
delete[] arr[i];
}
delete[] arr;
return 0;
}
The 2D array has been generated and shown on the console screen below.
Conclusion
That’s about the resized array in c++. We came to know that C++ arrays do not have a built-in method for resizing. But through the dynamic array allocation in c++, the array size can be modified. We have illustrated in the example to change the size of the dynamic array using a new keyword. Also, we can use an initializer list to initialize an array. After resizing we can also free up space in memory by using delete[]. This article will show you how to resize an array in C++.