Apache
Web server vs Application server: Key Differences
What is Server? A server is a central repository where data and computer programs are stored and...
A dynamic array is quite similar to a regular array, but its size is modifiable during program runtime. DynamArray elements occupy a contiguous block of memory.
Once an array has been created, its size cannot be changed. However, a dynamic array is different. A dynamic array can expand its size even after it has been filled.
During the creation of an array, it is allocated a predetermined amount of memory. This is not the case with a dynamic array as it grows its memory size by a certain factor when there is a need.
In this C++ tutorial, you will learn
The array's initial size and its growth factor determine its performance. Note the following points:
In C++, we can create a dynamic array using the new keyword. The number of items to be allocated is specified within a pair of square brackets. The type name should precede this. The requested number of items will be allocated.
The new keyword takes the following syntax:
pointer_variable = new data_type;
The pointer_variable is the name of the pointer variable.
The data_type must be a valid C++ data type.
The keyword then returns a pointer to the first item. After creating the dynamic array, we can delete it using the delete keyword.
#include<iostream> using namespace std; int main() { int x, n; cout << "Enter the number of items:" << "\n"; cin >>n; int *arr = new int(n); cout << "Enter " << n << " items" << endl; for (x = 0; x < n; x++) { cin >> arr[x]; } cout << "You entered: "; for (x = 0; x < n; x++) { cout << arr[x] << " "; } return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
NOTE: In the above example, the user is allowed to specify any size for the array during run time. This means the array's size is determined during runtime.
It's easy to initialize a dynamic array to 0.
Syntax:
int *array{ new int[length]{} };
In the above syntax, the length denotes the number of elements to be added to the array. Since we need to initialize the array to 0, this should be left empty.
We can initialize a dynamic array using an initializer list. Let's create an example that demonstrates this.
#include <iostream> using namespace std; int main(void) { int x; int *array{ new int[5]{ 10, 7, 15, 3, 11 } }; cout << "Array elements: " << endl; for (x = 0; x < 5; x++) { cout << array[x] << endl; } return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
The length of a dynamic array is set during the allocation time.
However, C++ doesn't have a built-in mechanism of resizing an array once it has been allocated.
You can, however, overcome this challenge by allocating a new array dynamically, copying over the elements, then erasing the old array.
Note: that this technique is prone to errors, hence, try to avoid it.
A dynamic array should be deleted from the computer memory once its purpose is fulfilled. The delete statement can help you accomplish this. The released memory space can then be used to hold another set of data. However, even if you do not delete the dynamic array from the computer memory, it will be deleted automatically once the program terminates.
Note:
To delete a dynamic array from the computer memory, you should use delete[], instead of delete. The [] instructs the CPU to delete multiple variables rather than one variable. The use of delete instead of delete[] when dealing with a dynamic array may result in problems. Examples of such problems include memory leaks, data corruption, crashes, etc.
#include<iostream> using namespace std; int main() { int x, n; cout << "How many numbers will you type?" << "\n"; cin >>n; int *arr = new int(n); cout << "Enter " << n << " numbers" << endl; for (x = 0; x < n; x++) { cin >> arr[x]; } cout << "You typed: "; for (x = 0; x < n; x++) { cout << arr[x] << " "; } cout << endl; delete [] arr; return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
What is Server? A server is a central repository where data and computer programs are stored and...
{loadposition top-ads-automation-testing-tools} Remote administration tools help IT professionals to debug...
What is the VI editor? The VI editor is the most popular and classic text editor in the Linux...
Video Hosting Sites are platforms that help you to upload, edit, and manage video clips with ease. These...
R is a programming language. To use R, we need to install an Integrated Development Environment...
YouTube is a popular video-sharing platform that helps users to watch, like, comment, and uploads...