Dynamic resizing of an array in C++
<p>I have a class with items pointing to an Airport object. The size of the dynamic array starts off at 10. When <code>total</code> is greater than the size of the array and I have read in data to fill up the array, I need to double the size of the array, copy the elements from the old array into the newly sized array and then delete the old array. The array will continue to read in data, then resize, and continue this until all the data from the file is read into the <code>items</code> array.</p>

<p>There are no errors that occur, but the problem is when I run the program, it crashes. I think my <code>doubleSize</code> function to resize the array might be the cause of this. Is there a way to fix this issue?</p>

<pre><code>class AirportList
{
private:
   Airport* items;
   int total;
   int maxElements;
   int oldAmount;
public:
     AirportList()
     {
        oldAmount = 10;
        maxElements = 10;
        items = new Aiport[maxElements];
        // a file was opened to read in data before this, 
        // total equals the total amount of lines in the file
        string cppstr;
     for (int counter = 0; counter &lt; total; counter++)
     {
        getline(infile, cppstr);
        items[counter] = Airport(cppstr);   // the Airport constructor                       
                                            // parses each line to read in   
                                           //  the data and sets each Airport
                                           //  object into the items array
        if (total &gt; maxElements &amp;&amp; counter == maxElements)
            doubleSize(items, maxElements, oldAmount);
     }
        infile.close();
     }

     void doubleSize(Airport*&amp; orig, int maxElements, int oldAmount)
     {
        maxElements = maxElements * 2;
        Aiport* resized = new Aiport[maxElements];
        for (int i = 0; i &lt; oldAmount; i++)
            resized[i] = orig[i];
        delete [] orig;
        orig = resized;
        oldAmount = maxElements;
     }

};
</code></pre>