c++ - 为什么删除分配的数组会导致内存错误?

标签 c++ arrays memory allocation

我出于教育目的实现了一个 ArrayList 类,但在我的 expand() 方法中删除数组时遇到了内存错误。

这是我的类(class)和所有重要的方法:

//create array with default size 2
template<class T>
ArrayList<T>::ArrayList(){
    realSize = 2;
    count = 0;
    data = new T[realSize];
}

//destructor
template<class T>
ArrayList<T>::~ArrayList() {
    delete []data;
}

//adds value to end of list
template<class T>
void ArrayList<T>::add(T val) {
    //if reached end of array, expand array
    if (count >= realSize)
        expand();
    data[count] = val;
    count++;
}

//inserts value at index
template<class T>
void ArrayList<T>::insert(T val, int index) {
    if (!isValid(index)) return;
    //if index is greater than current size, expand
    while (index >= realSize || count >= realSize) {
        expand();
    }
    //shift values before index
    for (int i = count; i >= index; i--) {
        T val = data[i];
        data[i + 1] = data[i];
    }
    data[index] = val;
    count++;
}

//return value at index
template<class T>
T ArrayList<T>::get(int index) {
    if (!isValid(index)) return 0;
    return data[index];
}

template<class T>
int ArrayList<T>::size() {
    return count;
}

template<class T>
void ArrayList<T>::expand() {
    //double array size
    realSize = realSize * 2;
    T* newData = new T[realSize];
    //replace data
    for (int i = 0; i < count; i++) {
        newData[i] = data[i];
    }
    delete[]data; //<--ERROR OCCURS HERE
    data = newData;
}

下面是一些会导致错误的代码

    ArrayList<int>* list = new ArrayList<int>();

    list->add(1);
    list->add(5);
    list->insert(2, 1);
    list->insert(3, 2);
    list->insert(4, 3); //<---ERROR OCCURS HERE

错误是一个消息框,上面写着

Debug Error!

Program: ...ommunity\Common7\IDE\Extensions\TestPlatorm\testhost.x86.exe

HEAP CORRUPTION DETECTED: after Normal block (#296) at 0x05D69BC0

CRT detected that the application wrote to memory after end of heap buffer.

为什么调用expand方法时偶尔会报错?据我所知,当调用 expand() 时,数组处于预期顺序(在我的示例中,它是 {1, 2, 3, 5})。

最佳答案

问题出在 insert 方法中。当您向上复制现有元素以为新元素腾出空间时,您从元素 count 开始,然后将 data[count] 向上复制一个槽位到 data [计数 + 1]。但是,data[count] 中没有存储任何元素,在正确的情况下,对 data[count + 1] 的访问将超过为 分配的空间数据.

那些情况发生在第二次 insert 调用中。 count 为 3,realsize 为 4,index 为 2,因此不会发生扩展。然后,您的 for 循环将分配 data[count + 1] = data[count],即 data[4] = data[3]。由于数据只有 4 个元素的空间,写入 data[4] 会破坏分配空间末尾的数据,这会在稍后的内存操作中检测到​​(在这种情况下,当分配的空间是通过调用 delete 释放)。

解决方案是从 int i = count - 1 开始你的循环,或者在条件下递减它:

for (int i = count; --i >= index; )

无关,T val = data[i]; 声明没有任何用处,可以删除。

关于c++ - 为什么删除分配的数组会导致内存错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59349168/

相关文章:

c++ - 使用声明(派生类)

c++ - Qt Creator 在尝试运行 OpenCV 程序时崩溃。 [ntdll.dll 崩溃]

java - 使用 Java Spring Boot 获取 Json 项目的值

c - 打印二维数组的无限循环

c++ - 如何在 C++ 中向量化 for 循环?

java - 分析内存不足错误的堆转储

java - 设置 int 值会导致性能问题

c++ - 在 C++ 中用指针和函数反转字符串

c++ - 在类中声明 tf2_ros::Buffer 时构建错误

javascript - 创建一个在 2 个不同值之间交替的数组