C++

How to Create a Dynamic Array of Integers in C++ Using new

Despite the array data structure provided by C++, traditional arrays have a fixed size and cannot be easily altered at runtime. However, C++ provides a solution with the new operator, allowing programmers to dynamically allocate memory for arrays. This powerful feature enables developers to create arrays of varying sizes based on runtime needs.

This tutorial will demonstrate how to utilize the new operator to dynamically create an array of integers in C++.

What is the new Operator in C++?

The new operator is a memory allocation request in C++. It initializes the allocated memory if enough space is available and returns the location of the initialized memory to the pointer variable. By defining the number of elements to be allocated inside the pair of square brackets, followed by the type name, and the new keyword in C++, we may create a dynamic array. The necessary number of elements are thus allocated as a result.

Note: We shall refer to both “new operator” and “new keyword” interchangeably throughout this article.

Syntax for the new Operator

The syntax for the new keyword is as follows:

int *array{new int[length]{}};

The length in the syntax above denotes how many additional members will be appended to the array. Because we must initialize the array to 0, we must leave this empty.

How to Use new Operator to Dynamically Create an Array of Integers in C++

Using the C++ new keyword, follow these steps to dynamically create an array of integers:

Step 1: First declare a pointer variable of integer type:

int* ptr;

Step 2: Then use the new keyword to allocate memory for the array:

ptr = new int[length];

The length is the number of entries that we need to store in an array.

Step 3: Then initialize an array like this:

ptr = new int[length]{1, 2, 3};

Step 4: Then use the for loop to iterate through the elements and cout function to print the array.

Step 5: Deallocate the memory once you are done with the array because it will overcome memory leaks in your program.

delete[] ptr;

The following example illustrates the above-given steps:

#include <iostream>
using namespace std;

int main() {
    int length = 5;
    int* ptr;
    ptr = new int[length]{1, 2, 3}; // allocates memory for array of length 5 and initializes first three elements to 1, 2, and 3, and the rest to 0
    for (int i = 0; i < length; i++) {
        cout << ptr[i] << " ";
    }
    cout << endl;
    delete[] ptr; // deallocates memory
    return 0;
}

The above demonstrates the use of the new keyword to create a dynamic array of integers in C++. The code initializes an array of length 5 with the first three elements set to 1, 2, and 3, and the rest to 0, and then prints the array using a for loop. The delete keyword is then used to deallocate the memory that was allocated to the array.

Output

Here is another example that takes input from the user for both the length of the array and its elements.

#include <iostream>
using namespace std;

int main() {
    int length;
    cout << "Enter the length of the array: ";
    cin >> length;

    int* ptr = new int[length];

    for (int i = 0; i < length; i++) {
        cout << "Enter element " << i << ": ";
        cin >> ptr[I];
    }

    cout << "The array elements are: ";
    for (int i = 0; i < length; i++) {
        cout << ptr[i] << " ";
    }
    cout << endl;

    delete[] ptr;

    return 0;
}

In the above example, the user is prompted to enter the length of the array, and then the program allocates memory for the array using the new operator. The program then iterates through the array, asking the user to put each entry. Finally, the program outputs the array elements and deallocates the memory.

Output

Conclusion

The new operator in C++ allows programmers to dynamically allocate memory for arrays, enabling the creation of arrays of varying sizes based on runtime needs. By following the steps outlined in this article, C++ programmers can easily create a dynamic array of integers and initialize it to their desired values. To prevent memory leaks in the program, it is essential that you know to deallocate the memory.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.