Search code examples
c++arrayspointersmemorydynamic-arrays

Memory Error thrown while trying to create dynamic arrays in C++


For my assignment I need to make a dynamic array class which utilizes an addEntry and deleteEntry function. I've been looking for a while for this solution and can't find it. My code is as follows:

//headers
#include <iostream>
#include <string>
using namespace std;

//Class Declaration
class DynamicStringArray {
    //private variables
private:
    //needs to be a pointer
    string *dynamicArray;
    int size;
public:
    //public functions
    DynamicStringArray();
    DynamicStringArray(const DynamicStringArray& array);
    int getSize();
    void addEntry(string entry);
    bool deleteEntry(string entry);
    string getEntry(int index);
    void operator= (const DynamicStringArray& equals);
    ~DynamicStringArray();
};

//default constructor
DynamicStringArray::DynamicStringArray() {
    dynamicArray = new string[0];
    size = 0;
}

//copy constructor
DynamicStringArray::DynamicStringArray(const DynamicStringArray& array) {
    //makes their size equal
    size = array.size;
    dynamicArray = new string[size];
    //copies the data over.
    for (int i = 0; i < size-1; i++) {
        dynamicArray[i] = array.dynamicArray[i];
    }
}

//accessor method to get the size
int DynamicStringArray::getSize() {
    return size;
}

//adding an entry
void DynamicStringArray::addEntry(string entry) {
    string *tempArray = new string[size + 1];
    for (int i = 0; i < size; i++) {
        tempArray[i] = dynamicArray[i];
    }
    //increment size
    size++;
    tempArray[size] = entry;
    //set dynamic array to the temp
    delete[] dynamicArray;
    dynamicArray = tempArray;
    //cleanup
    delete[] tempArray;
}

//delete entry
bool DynamicStringArray::deleteEntry(string entry) {
    bool found = false;
    bool replaced = false;
    int index;
    //checks for the value
    for (int i = 0; i < size; i++) {
        if (dynamicArray[i] == entry) {
            found = true;
            index = i;
        }
    }
    if (!found) {
        return found;
    }
    //declaring temp array
    string *tempArray = new string[size - 1];
    //copies data over, skipping over the one not being coppied.
    for (int i = 0; i < size; i++) {
        if (!replaced) {
            tempArray[i] = dynamicArray[i];
        }
        else if (replaced) {
            tempArray[i - 1] = dynamicArray[i];
        }
        if (i == index) {
            replaced = true;
        }
    }
    //setting dynamic array to the temp 
    delete[] dynamicArray;
    dynamicArray = tempArray;
    //de-increment
    size--;
    //cleanup
    delete[] tempArray;
    return true;
}

//accessor method to get the value at an index.
string DynamicStringArray::getEntry(int index) {
    return dynamicArray[index];
}

void DynamicStringArray::operator= (const DynamicStringArray& equals) {
    //makes their size equal
    size = equals.size;
    dynamicArray = new string[size];
    //copies the data over.
    for (int i = 0; i < size; i++) {
        dynamicArray[i] = equals.dynamicArray[i];
    }
}

DynamicStringArray::~DynamicStringArray() {
    delete[] dynamicArray;
    dynamicArray = NULL;
}
int main()
{
    DynamicStringArray* test = new DynamicStringArray;
    cout << test->getSize() << endl;
    test->addEntry("joe");
    cout << test->getSize() << endl;
    return 0;
}

I commented out the addEntry and deleteEntry functions and it compiles fine. After I uncommented them out, I uncommented "test->addEntry("joe");" in the main method and it compiled fine.

The error thrown is:

Exception thrown: read access violation.
_Pnext was 0xFDFDFE01.

in the xmemory file, which I believe is just part of the visual studio library.

If there is any additional information you need please let me know.


Solution

  • There are two problems with addEntry. You have one allocation (call to new) with two deletes, which is fixed by removing the delete [] tempArray; from the end of the function (with it there, you delete the newly allocated memory leaving dynamicArray pointing to memory that is no longer allocated).

    The other is that you increment size too soon. You need to assign to tempArray[size] first, then increment size.