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

问题描述 投票:-1回答:1

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

这是我的课程和所有重要的方法:

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

错误是显示为的消息框

调试错误!

程序:... ommunity \ Common7 \ IDE \ Extensions \ TestPlatorm \ testhost.x86.exe

检测到堆损坏:在0x05D69BC0的正常块(#296)之后

CRT检测到应用程序在堆缓冲区结束后写入了内存。

为什么在调用expand方法时偶尔会导致错误?据我所知,调用expand()时,数组按预期顺序排列(在我的示例中为{1, 2, 3, 5})。

c++ arrays memory allocation
1个回答
1
投票

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

这些情况发生在第二个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];声明没有任何用处,可以删除。

© www.soinside.com 2019 - 2024. All rights reserved.