C++ All-in-One For Dummies
Book image
Explore Book Buy On Amazon

Static arrays are allocated on the stack, which can limit their usability. Dynamic arrays are allocated on the heap, which means they're only limited by the size of memory. Admittedly, you'll find a few other differences between dynamic and static arrays, but the bottom line here is that, although dynamic arrays require a little more work to use because you must manage the memory yourself, they also provide added flexibility in working with data.

More than likely you'll want to handle complex data when making use of dynamic arrays. The example in this online article aims to please by looking at how you can use a dynamic array with a structure. Here is an example of the code you might use:

#include 
#include 
using namespace std;
struct Employee
{
    string Name;
    int Age;
};
int main()
{
    Employee* DynArray;
    DynArray = new (nothrow) Employee[3];
    DynArray[0].Name = "Harvey";
    DynArray[0].Age = 33;
    DynArray[1].Name = "Sally";
    DynArray[1].Age = 26;
    DynArray[2].Name = "Jeff";
    DynArray[2].Age = 52;
    cout << "Displaying the Array Content" << endl;
    for (int i = 0; i < 3; i++)
    {
        cout << "Name: " << DynArray[i].Name <<
            "tAge: " << DynArray[i].Age << endl;
    }
    delete[] DynArray;
    return 0;
}

In this example, the code begins by creating an Employee struct that contains the employee name and age. You could use any sort of data container desired — this one just happens to be a struct.

In order to create a dynamic array, you define a pointer to the array variable. This act places the variable on the heap, rather than the stack. You then create the array, which contains three Employee entries in this case. The code fills in the data and then uses a loop to display the results on screen. Here is what you should see when you run the example.

Displaying the Array Content
Name: Harvey    Age: 33
Name: Sally     Age: 26
Name: Jeff      Age: 52

Notice that you access the individual members of Employee by accessing the required array index and then using dot syntax to specify the member name. It's important to remember that the dynamic array acts like any other array in that you access an index to obtain a specific entry.

It's essential to use delete[] to free the memory used by DynArray. Otherwise, your application will have a memory leak that will eventually affect system performance or at least cause it to do funny things. Because our systems are already funny acting enough, it's probably a good idea to ensure your application doesn't' contribute to the problem.

About This Article

This article is from the book:

About the book author:

John Mueller has published more than 100 books on technology, data, and programming. John has a website and blog where he writes articles on technology and offers assistance alongside his published books.

Luca Massaron is a data scientist specializing in insurance and finance. A Google Developer Expert in machine learning, he has been involved in quantitative analysis and algorithms since 2000.

This article can be found in the category: