Home

Using a Dynamic Array with a Structure

|
|  Updated:  
2016-03-26 11:10:14
C++ For Dummies
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 <iostream>
#include <new>
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 Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.

Jeff Cogswell has been an application developer and trainer for 18 years, working with clients from startups to Fortune 500 companies. He has developed courses on C++ and other technologies.