0

I have a class with items pointing to an Airport object. The size of the dynamic array starts off at 10. When total 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 items array.

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

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 < 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 > maxElements && counter == maxElements)
            doubleSize(items, maxElements, oldAmount);
     }
        infile.close();
     }

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

};
4
  • ` I think my doubleSize function to resize the array might be the cause of this` Well debug it and confirm that is the case! First obvious question: is the oldAmount parameter to doubleSize supposed to be a reference? If not then setting it on the last line does nothing. (ditto for maxElements. I think that HAS to be a ref, oldAmount not so important. Commented Apr 10, 2016 at 23:59
  • @John3136 I tried debugging it, but I am still new to debugging, so I couldn't find out what's causing the crash unfortunately. Commented Apr 11, 2016 at 0:06
  • You can debug just by printing out values to see how far your program gets and what is it's state at different points. Commented Apr 11, 2016 at 0:09
  • The code isn't correct. You must declare the array as Airport ** items Commented Apr 11, 2016 at 0:38

2 Answers 2

3

You don't double the size until after you've made an assignment to the array where counter == maxelements, but items[maxelements] isn't valid -- only maxelements - 1.

If you move doublesize up above the assignment, things should work better.

Also, there's no reason to check if total > maxElements. It's simply not necessary.

Sign up to request clarification or add additional context in comments.

17 Comments

doubleSize(items, maxElements, oldAmount); if (counter == maxElements - 1) doubleSize(items, maxElements, oldAmount); Is this what you mean by moving it up above the assignment?
@Mark You can NEVER assign to items[current] when current==maxsize -- remember arrays start at 0, so a size=3 array has valid elements at 0, 1, and 2. You need to increase the size before you make that assignment. So just move the if block above the assignment. readline then if(current==maxsize){...} then items[current]=...
I understand now. The AirportList class does not have a copy constructor, destructor, and overloaded = operator as seen from above. Do you think that also could be causing the crashes?
Depends if you're making copies of it -- and if so what you do with them. Something should probably be calling delete[] on items when it's destroyed, but that won't cause a crash, just a memory leak. In general, if you dynamically allocate memory, you should have a destructor and either a custom copy constructor (or explicitly delete it with the "= delete;" option)
I have modified it to if (counter == maxElements - 1) { double the size }. Then readline then items[counter] = Airport(cppstr);. However, it still crashes. Could I still have misunderstood you?
|
0

I suspect you have an error with the copy constructor for the Airport class. You need to define a copy constructor for Airport (which does a deep copy of its data) and overload the assignment operator to use the new copy constructor. Otherwise, when you do resized[i] = orig[i];, you will do a shallow copy which ends up still pointing to data in the original (now deleted) Airport object.

Take a look here: http://www.cplusplus.com/articles/y8hv0pDG/

3 Comments

You don't think it could be related to the assignment into items[counter] when counter == maxelements?
My Airport class does not have a copy constructor since I don't have dynamic memory in that class. Do I still need a copy constructor?
@xaxxon missed that -- yes you definitely need to resize BEFORE calling items[counter] when counter == maxelements!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.