Creating and Using Dynamic Arrays - C++ Operator

C++ examples for Operator:new

Description

Creating and Using Dynamic Arrays

Demo Code

#include <iostream>
#include <new>

using namespace std;

int main()/*from   w  w w . j  av a2 s  .  c  o m*/
{
    int HowMany;
    int* DynArray;

    cout << "How many numbers would you like?" << endl;
    cin >> HowMany;

    DynArray = new (nothrow) int[HowMany];

    if (DynArray == nullptr)
        cout << "Error: Could not allocate memory!";
    else
    {
        for(int i = 0; i < HowMany; i++)
            DynArray[i] = i;

        cout << "Displaying entries:" << endl;
        for (int i = 0; i < HowMany; i++)
            cout << DynArray[i] << endl;

        delete[] DynArray;
    }
    return 0;
}

Result


Related Tutorials