C++访问冲突读取位置0xDDDDDDCD当我尝试删除已更新的数组时
C++ Access Violation reading location 0xDDDDDDCD when I try to delete an array UPDATED
我正在做家庭作业。我正在尝试重载我正在创建的 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;
因此,在该调用之后,arr2
和arr
都持有相同的指针(指向同一对象(。从那时起,delete arr2;
将删除您之前delete arr;
两行时已经删除的同一对象:
delete arr;
arr = nullptr;
delete arr2;
因此,在这里delete arr2;
这样做会导致已经未定义的行为。在这一点上,任何事情都可能发生。
相关文章:
- 如何删除已输入的书以及如何使标题,语言和名称在上面放置空格不会出错?
- 在删除之前更新一组元素指针中的元素
- 从矢量中删除已完成的线程
- Denstructor删除已被替换的成员
- 如何通知侦听器结构的哪个字段已更新
- 如何确保我的"ifstream"文件对象指向的文件内容已更新?
- C++访问冲突读取位置0xDDDDDDCD当我尝试删除已更新的数组时
- 使用 MFC c++ 删除已装载驱动器的文件
- 有没有一种有效的方法来删除已分配但空的特征行而不重新分配
- 删除和更新矢量<struct>中的特定元素
- 如何检查输入文件是否已更新
- 是否会自动删除已添加到重复字段的指针
- 删除已排序链表中的值
- 应由库或客户端代码删除已加载库中的对象实例
- 始终删除已分配的动态内存
- 删除已创建的Qt元素和子类
- Visual C++ 2012 应用程序无法在 Windows XP 下运行 - 已更新
- 如何通知父 CDialog 子 CWnd 已更新,以便另一个子控件可以更新作为响应
- 已更新:从函数返回指向数组的指针
- 整数和无效操作数中的小数*已更新