0

I cannot figure out why this keeps crashing on me. The code makes it almost to the end. Specifically it makes it to the print(copyArray); line. I have no idea where I went wrong, but it keeps popping up the same error.

I have tried calling the destructors, but apparently I am not doing it right.

elementArray.~ManagedArray();

EDIT:

I read that entire thread you posted and still cannot find the answer. I did however figure out if I comment out the print functions it does not occur. But once I print it crashes at the end of the function.

Error I am receiving

#include <iostream>
using namespace std;

class ManagedArray
{
    private:
        int numberOfElements;
        float *elements;


    public:


        ManagedArray(): numberOfElements(0), elements(NULL){}
        int size();
        float get(int index);
        void add(float value);
        ManagedArray(ManagedArray & ma);
        ~ManagedArray() {delete [] elements;}




};


ManagedArray::ManagedArray(ManagedArray & ma):numberOfElements(ma.numberOfElements) 
{
    elements = ma.elements;
}

int ManagedArray::size()
{
    return numberOfElements;
}

float ManagedArray::get(int index)
{
    return elements[index];
}

void ManagedArray::add(float value)
{
    float * BiggerArray = new float[numberOfElements+1];
    if(elements != NULL)
    {
        // copy the old elements into the biggerArray
        for(int i = 0; i < numberOfElements; i++)
        {
            BiggerArray[i] = elements[i];
        }
        // the old array is not needed anymore, we have a better copy
        this->~ManagedArray();
    }
    elements = BiggerArray;
    numberOfElements = numberOfElements+1;
    // put the new number into the last element of the array
    elements[numberOfElements-1] = value;
}

void print(ManagedArray ma)
{
    // print the stored numbers
    cout << "Entered numbers: " << endl;
    cout << "{";
    for(int i = 0; i < ma.size(); ++i)
    {
        if(i > 0)
        {
            cout << ", ";
        }
        cout << ma.get(i);
    }
    cout << "}" << endl;

    float sum = 0;
    for(int i = 0; i < ma.size(); ++i)
    {
        sum += ma.get(i);
    }
    cout << "total: " << sum << endl;
    cout << "average: " << (sum / ma.size()) << endl;
}

int main()
{
    float userInput;
    ManagedArray elementArray;
    bool addingNumbersToTheList;
    cout << "Keep entering numbers. Enter a non-number to stop." << endl;
    do
    {
        cin >> userInput;
        addingNumbersToTheList = !std::cin.fail();
        if(addingNumbersToTheList) {
            elementArray.add(userInput);

        }
    }
    while(addingNumbersToTheList);

    // fix cin after intentionally breaking it above.
    if(std::cin.fail())
    {
        std::cin.clear();
        while(std::cin.get() != '\n');
    }
    bool hasNumbers = elementArray.size() > 0;
    if(hasNumbers) {
        ManagedArray copyArray(elementArray);
        cout << "printing elementArray:\n";
        print(elementArray);
        cout << "\nprinting copyArray:\n";
        print(copyArray);



    }
    else
    {
        cout << "no numbers entered." << endl;
    }
    cin.get();
    cin.get();
    return 0;
}
  • 2
    When you thoroughly read [ask] just before posting this question, you were instructed to create a MCVE that would also probably let you find the problem yourself. Why didn't you do that? – Amit Jan 24 '16 at 22:42
  • This error means you are corrupting the heap. Perhaps you are deleting the array more than 1 time. Perhaps you are improperly copying objects. Perhaps you are stepping outside the bounds of the array. – drescherjm Jan 24 '16 at 22:45
  • 2
    Don't do this `this->~ManagedArray();`. – Leiaz Jan 24 '16 at 22:47
  • You just copy elements pointer in copy constructor. Allocate new memory and copy elements manually. – Michael Nastenko Jan 24 '16 at 22:48
  • Your copy constructor is wrong. You did not make a deep copy. – drescherjm Jan 24 '16 at 22:49
  • 2
    BTW, `void print(ManagedArray ma)` isn't good. You create array copy just to print it out. In this case use const ref `void print(const ManagedArray& ma)` – Michael Nastenko Jan 24 '16 at 22:52
  • The copy constructor was just a way to test it. The error still occurs without it. – Jeremy Johnson Jan 24 '16 at 23:27
  • It will have this problem if you do not implement a copy constructor. The default compiler implemented copy constructor will not do a deep copy. You need to understand and implement the rule of three. – drescherjm Jan 25 '16 at 00:13
  • It would be best style to have a minimal possible class to manage the memory, and then ManagedArray can have one of those as a data member and follow Rule of Zero. You could even use `vector` as the inner class since you intend to resize. – M.M Jan 25 '16 at 03:27

0 Answers0