gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:41:21 24 4
gpt4 key购买 nike

对于我的作业,我需要创建一个动态数组类,它使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com