C++

Dynamic Char Array C++

A dynamic array is comparable to a standard array, except its size can be changed while the program is running. The members of a Dynamic Array take up a single memory cache, once it has been completed, a dynamic array might grow exponentially. Specified cache memory is allocated to an array when it is created. A dynamic array, on the other hand, extends its memory capacity by a specific criterion when it is required. We employ a dynamic array when we need to assess the size at runtime. In this article, let’s discuss the details of the dynamic character array in C++.

Utilize the new() operator:

The new operator dynamically provides the object on the main memory and reverts a pointer to it. A character array is declared in this subsequent program. After that, in a for loop, we efficiently allocate the char array and specify the appropriate data to its components.

#include<iostream>

using namespace std;
int main()
{
    int i, a;
    cout<< "Enter the number of values:" <>a;
    int *arr = new int(a);
    cout<< "Enter " << a << " values" <<endl;
    for (i = 0; i<a>>arr[i];
    }
    cout<< "You entered: ";
    for (i = 0; i< a; i++)
    {
        cout<<arr[i] << " ";
    }
    return 0;
}

Here, we are going to integrate the header file <iostream> to utilize its functionalities. To utilize the classes in the program without declaring it, we have used a standard namespace. The main() function is being called in the next step.

First, we declare two variables ‘i’ and ‘a’. We utilize the ‘cout’ statement to print the line on the screen so the user enters the amount of numbers he wants to acquire. Then, this value is assigned to the variable ‘a’. Now, we acquire an array that contains the value of the ‘a’ variable and then assign this to a pointer of the array. Once again ‘cout’ command is being used to allow the user to enter any random numbers.

For loop is executed which initializes a loop variable ‘i’ to recapitulate the numbers entered by the user. The numbers within the array ‘arr’ are now displayed. On successful execution, the program will return value. The main() function’s body has come to an end.

Utilize an Initializer list:

It’s simple to set a dynamic character array to 0. The length indicates the number of items to be inserted into the array in this methodology. The array will be left blank because we have to specify the character array to zero. An initializer list would be used to create a dynamic character array. Take a look at an example.

#include<iostream>

using namespace std;
int main(void)
{
    int j;
    int *array{ new int[8]{ 1, 14, 9, 0, 33, 5, 28, 6 } };
    cout<< "Elements of the array: " <<endl;
    for (j = 0; j < 8; j++)
    {
        cout<< array[j] <<endl;
    }
    return 0;
}

First, we include the header file <iostream> for input and output functions. The standard namespace is also utilized so that we can access the classes without having to call it. We employ the main() function. Inside the body of this function, the variable ‘j’ is initialized. Then, we declare an array with a list. The data type of this array is an integer. The array contains 8 numeric integers. We want to print the line ‘Elements of the array’ on the screen so we utilize the ‘cout’ statement. The ‘endl’ command shows the end of the line. It just moves the output to the next line.

We apply the ‘for’ loop. Within the ‘for’ loop we just initialize a loop variable ‘j’ then we specify the condition that the value of the variable ‘j’ must be less than 8. In the last part, we do increase the value of the loop. To display the elements of the defined array on the screen, ‘cout’ statement is being used. Outside the ‘for’ loop we enter the ‘return 0’ command to end the program.

Utilize the std::unique_ptr method:

The std::unique_ptr pointer is another approach in creating a dynamic character arrayand it facilitates a secure memory allocation interface. The item to which the unique_ptr function points have to be owned by the operation; but, if the pointer exits the range, the element is discarded. Unlike conventional pointers, the smart pointer does not require a programmer to execute the delete operator; rather, it is called implicitly whenever the element is eliminated.

#include<iostream>

#include<memory>

using std::cout;
using std::endl;
constexpr int s = 11;
static const char chars[] =
{ 'I', 'n', 'F', 'O', 'r', 'm', 'a', 'T', 'I', 'o', 'N' };
int main()
{
std::unique_ptrarr(new char[s]);
for(int k = 0; k < s; ++k)
    {
arr[k] = chars[k];
cout<<arr[k] << "; ";
    }
cout<<endl;
    return EXIT_SUCCESS;
}

At the start of the program, we introduce two required libraries: <iostream> and <memory>. To utilize the output functions, we utilize standard ‘cout’. Along with this, we have been using ‘endl’ which shows the end of the line. It just moves the cursor to the succeeding line. The size of the array is specified here by using an integer data type.

In the next step, we declare the static constant character array to define its elements. We invoke the main() function. And for the memory allocation, we apply std::unique_ptr within the body of the function. We construct a loop variable ‘k’ within for loop to go over the values of the defined array. Then, Retrieve the defined values of the array and store them in the variable ‘arr’. To display the characters contained in the ‘arr’ the ‘cout’ statement is being used. To accomplish, the code may return value. In the last step, we use ‘return EXIT_SUCCESS’ to terminate the code.

Conclusion:

In this article, we have talked about the dynamic character array and different methodologies of allocating the character arrays in C++. These techniques include the use of a new() operator, initializer list, and std::unique_ptr method. We utilize a dynamic array when we need to determine the size of the character array at runtime. The length of a dynamic character array is determined at the moment of the allocation.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content