c++ - 尝试在 C++ 中创建动态数组时抛出内存错误

标签 c++ arrays pointers memory dynamic-arrays

对于我的作业,我需要创建一个动态数组类,它使用 addEntry 和 deleteEntry 函数。我一直在寻找这个解决方案,但找不到。我的代码如下:

//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;
}

我注释掉了 addEntry 和 deleteEntry 函数,它编译得很好。在我取消注释它们之后,我取消注释“test->addEntry(“joe”);”在主要方法中,它编译得很好。

抛出的错误是:

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

在 xmemory 文件中,我相信它只是 visual studio 库的一部分。

如果您需要任何其他信息,请告诉我。

最佳答案

addEntry 有两个问题。您有一个分配(调用 new)和两个删除,这是通过从函数末尾删除 delete [] tempArray; 来修复的(有了它,您删除新分配的内存,使 dynamicArray 指向不再分配的内存。

另一个是您过早地增加了 size。您需要先分配给 tempArray[size],然后递增 size。

关于c++ - 尝试在 C++ 中创建动态数组时抛出内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59132009/

相关文章:

c - 用随机数和字母初始化c中的2d矩阵

c++ - 删除 vector 中的指针后出现意外行为

c++ - C++ 中的 Iterator 不是一种指针吗?

c++ - 为什么它停止并以退出代码 11 结束?

c++ - 从 std::vector 连续删除的安全方法?

c++ - 如何使用两种不同的返回类型重载 []

c++ - 为什么 offsetof 实现在 C 和 C++ 上有奇怪的不同?

python - 在 python numpy 中指定二维数组的大小

ios - Swift - 优化使用非常长的数组

javascript - JS : Check string if it is failing for some rules