C++访问冲突读取位置0xDDDDDDCD当我尝试删除已更新的数组时

C++ Access Violation reading location 0xDDDDDDCD when I try to delete an array UPDATED

本文关键字:删除 已更新 数组 读取 访问冲突 位置 0xDDDDDDCD C++      更新时间:2023-10-16

我正在做家庭作业。我正在尝试重载我正在创建的 Array 类的"="运算符,以便它将分配一个新创建的数组,其值与另一个数组相同。这似乎有效。将创建数组并复制数据。我还检查了数组第一个元素的位置,它与原始元素不同,因此我认为它不会尝试删除已删除的数组。

我试过弄乱我的析构函数,但老实说我不知道这是从哪里来的。如果有人有任何可能有帮助的调试策略,我也很乐意听到它们。

驱动程序.cpp


int main ()
{
//Initialize 
int size = 0;
char fill = '';
//Get info about the array
std::cout << "How long should the array be?" << std::endl;
std::cin >> size;
std::cout << "Choose fill character." << std::endl;
std::cin >> fill;
//Create array & Print array details
Array* arr = new Array(size, fill);
std::cout << "The array size is: " << arr->size() << std::endl;
std::cout << "max size: " << arr->max_size() << std::endl;
std::cout << "The contents of the array is: ";
arr->printArr();
std::cout << std::endl;
//Create new array & set it's values equal to old array
Array* arr2 = new Array();
arr2 = arr;
//= OVERLOAD TESTING
std::cout << "The array size is: " << arr2->size() << std::endl;
std::cout << "max size: " << arr2->max_size() << std::endl;
std::cout << "The contents of the array is: ";
arr2->printArr();
//Deallocate memory 
delete arr;
arr = nullptr;
delete arr2;
arr2 = nullptr;
//Checking for memory leaks 
_CrtDumpMemoryLeaks();
return 0;
}

数组.cpp文件

//Define MAX SIZE so that it can be easily changed.
#define MAX_SIZE_ 200
#include "Array.h"
#include <iostream>
#include <stdexcept>
Array::Array (void)
:data_ (new char[MAX_SIZE_]), 
cur_size_ (0), 
max_size_ (MAX_SIZE_) 
{   }
//Overloaded Constructor 
//Assigns the initial size of the array and fills each element with the character stored in fill.
Array::Array (size_t length, char fill)
: data_ (new char[length]), 
cur_size_ (length), 
max_size_ (length) 
{   
//Fill each element with the character passed in to the function.
for(int i = 0; i < length; i++)
{
this-> data_[i] = fill; 
}
std::cout << &this->data_ << std::endl;
} 
//Destructor
Array::~Array (void)
{
delete[] this->data_;
this->data_ = nullptr;
}
//Sets new array equal to rhs.
const Array & Array::operator = (const Array & rhs)
{
//Set current and max size values to new array.
this->max_size_ = rhs.max_size_;
this->cur_size_ = rhs.cur_size_;
//Copy data from rhs.data_ to new array's data_
for(int i = 0; i < rhs.cur_size_; i++)
{
this->data_[i] = rhs.data_[i];
}
return *this;
}
//Print the contents of the array.
void Array::printArr(void)
{
for (int i = 0; i < (this->cur_size_) ; i++)
{
std::cout << this->data_[i];
}
}

Expected Results: The program displays information about the different arrays, then deletes them with no memory leaks.
Actual Results: The program displays all the correct data for both arrays and is able to delete the first array without a hitch, but runs into an exception when calling:
delete[] this->data_;

on the second array.
> Exception thrown at 0x5D13DB1B (ucrtbased.dll) in driver.exe: 0xC0000005: Access violation reading location 0xDDDDDDCD
Thanks for any help!

当你做arr2 = arr;你复制arr保持的指针(记忆地址(到arr2

Array* arr2 = new Array();
arr2 = arr;

因此,在该调用之后,arr2arr都持有相同的指针(指向同一对象(。从那时起,delete arr2;将删除您之前delete arr;两行时已经删除的同一对象:

delete arr;
arr = nullptr;
delete arr2;

因此,在这里delete arr2;这样做会导致已经未定义的行为。在这一点上,任何事情都可能发生。