How to Dynamically Allocate an Array in C++

  1. Use the new() Operator to Dynamically Allocate Array in C++
  2. Use the std::unique_ptr Method to Dynamically Allocate Array in C++
  3. Use std::make_unique() Method to Dynamically Allocate Array in C++

This article demonstrates multiple methods on how to dynamically allocate an array in C++.

Use the new() Operator to Dynamically Allocate Array in C++

The new operator allocates the object on the heap memory dynamically and returns a pointer to the location. In this example program, we declare the constant character array and size as an int variable. Then, we dynamically allocate the char array and assign the corresponding values to its elements in the for loop body.

Note that the delete operator must be explicitly called once the memory associated with the arr pointer is no longer needed. The specified brackets after the delete operator tell the compiler that the pointer refers to the first element of an array. If the brackets are omitted when deleting a pointer to an array, the behavior is undefined.

#include <iostream>

using std::cout;
using std::endl;

constexpr int SIZE = 10;
static const char chars[] =
        { 'B', 'a', 'd', 'C', 'T', 'L', 'A', 'R', 'e', 'I' };

int main()
{
    char *arr = new char[SIZE];

    for(int i = 0; i < SIZE; ++i) {
        arr[i] = chars[i];
        cout << arr[i] << "; ";
    }
    cout << endl;

    delete [] arr;
    return EXIT_SUCCESS;
}

Output:

B; a; d; C; T; L; A; R; e; I;

Use the std::unique_ptr Method to Dynamically Allocate Array in C++

Another way to allocate a dynamic array is to use the std::unique_ptr smart pointer, which provides a safer memory management interface. The unique_ptr function is said to own the object it points; in return, the object gets destroyed once the pointer goes out of the scope. Contrary to the regular pointers, the smart pointer doesn’t need the delete operator to be called by a programmer; instead, it’s called implicitly when the object is destroyed.

#include <iostream>
#include <memory>

using std::cout;
using std::endl;

constexpr int SIZE = 10;
static const char chars[] =
        { 'B', 'a', 'd', 'C', 'T', 'L', 'A', 'R', 'e', 'I' };

int main()
{
    std::unique_ptr<char[]> arr(new char[SIZE]);

    for(int i = 0; i < SIZE; ++i) {
        arr[i] = chars[i];
        cout << arr[i] << "; ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

B; a; d; C; T; L; A; R; e; I;

Use std::make_unique() Method to Dynamically Allocate Array in C++

The make_unique function is a more contemporary alternative to handle dynamic memory management. This method dynamically allocates an object of a given type and returns the std::unique_ptr smart pointer. The type of the object is specified like the template parameter. On the other hand, the char array of fixed size is allocated and assigned to the auto type variable in the following example:

#include <iostream>
#include <memory>

using std::cout;
using std::endl;

constexpr int SIZE = 10;
static const char chars[] =
        { 'B', 'a', 'd', 'C', 'T', 'L', 'A', 'R', 'e', 'I' };

int main()
{
    auto arr = std::make_unique<char[]>(SIZE);

    for(int i = 0; i < SIZE; ++i) {
        arr[i] = chars[i];
        cout << arr[i] << "; ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

B; a; d; C; T; L; A; R; e; I;

Related Article - C++ Array

  • How to Pass 2D Array to a Function in C++
  • How to Return an Array From a Function in C++