首页 > 解决方案 > 当我尝试删除数组时,C++ 访问冲突读取位置 0xDDDDDDCD 已更新

问题描述

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

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

驱动程序.cpp


    int main ()
    {
            //Initialize 
        int size = 0;
        char fill = '\0';

            //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!

标签: c++visual-studiodebugging

解决方案


当你这样做时,你将指针(内存地址)arr2 = arr;复制到:arrarr2

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

所以在那次调用之后,两者都arr2持有arr相同的指针(指向同一个对象)。届时将delete arr2;删除您之前执行两行时已删除的同一对象delete arr;

delete arr;
arr = nullptr;
delete arr2;

所以在delete arr2;这里这样做会导致已经未定义的行为。到那时,任何事情都可能发生。


推荐阅读