简体   繁体   English

删除动态分配的对数组,引发无效地址错误

[英]Deleting a dynamically allocated Array of Pairs throwing invalid address error

I have a very curious problem. 我有一个很好奇的问题。 I have a custom class Set , and a custom class Map (I have to recreate the standard library implementations for a class). 我有一个自定义类Set和一个自定义类Map (我必须为一个类重新创建标准库实现)。 In my map class, I create an array of pair<string, Set<string>> . 在地图类中,我创建一个pair<string, Set<string>>数组。 But, when I expand my array of values and re-hash the values, I want to delete my old array. 但是,当我扩展值数组并重新散列值时,我想删除旧的数组。 But, Whenever (and wherever in my code...) I try to, I get a Invalid address specified to RtlValidateHeap error. 但是,无论何时(无论我的代码中什么地方...),我都会尝试Invalid address specified to RtlValidateHeap错误Invalid address specified to RtlValidateHeap一个Invalid address specified to RtlValidateHeap This happens even when the delete call is on the line after my new[] statement. 即使在我的new[]语句之后的行上执行delete调用,也会发生这种情况。

I have private class variable I call pair<string, Set<string>> *values; 我有私有类变量,我称之为pair<string, Set<string>> *values; . Then in my constructor I do the following. 然后在我的构造函数中执行以下操作。

values = new pair<string, Set<string>>[tableSize];

Then when I got to delete values in a member function it threw the invalid address error. 然后,当我要删除成员函数中的值时,它抛出了无效的地址错误。 The code is below - I swap newValues , and values , then delete newValues in the reallocate() function. 下面的代码-我交换newValuesvalues ,然后在reallocate()函数中delete newValues That is where the error is thrown 那就是抛出错误的地方

Node: the map is functioning perfectly. 节点:地图运行正常。 I can hash, store, and recall values without any errors. 我可以散列,存储和调用值,而不会出现任何错误。

Expanded Code: 扩展代码:

template<>
class Map<std::string, Set<string>> : public MapInterface<string,Set<string>>{
public:

Map() {
    numItems = 0;
    tableSize = BonusHashTableSize;
    values = new pair<string, Set<string>>[tableSize];
    for (int i = 0; i < tableSize; ++i) {
        values[i].first = "";
    }
};

~Map() {
    for (int i = 0; i < tableSize; ++i) {
        if (values[i].first != "") {
            values[i].second.clear();
        }
    }
    delete[] values;
};


void reallocate() {
    tableSize *= 2;
    pair<string, Set<string>> *newValues = new pair<string, Set<string>>[tableSize];
    for (int i = 0; i < tableSize; ++i) {
        newValues[i].first = "";
        newValues[i].second = Set<string>();
    }
    for (int i = 0; i < tableSize / 2; ++i) {
        if (values[i].first != "") {
            int newIndex = rehash(newValues, values[i].first);
            newValues[newIndex].first = values[i].first;
            newValues[newIndex].second = values[i].second;
            Set<string> test = newValues[newIndex].second;
        }
    }
    std::swap(values, newValues);
    delete[] newValues;

//member functions

private:
pair<string, Set<string>> *values;
int tableSize;
int numItems;
};

Remove delete values from constructor. 从构造函数中删除删除值。 What is the purpose of allocation if you are just going to delete in the next line? 如果只想在下一行删除,分配的目的是什么? And also you are using the deleted memory in the next for loop block. 而且,您还在下一个for循环块中使用已删除的内存。

Since you have deleted it here , it will cause invalid memory access error anywhere else because you are trying to delete already deleted memory location. 由于您已在此处删除它,它将在其他任何地方导致无效的内存访问错误,因为您正试图删除已删除的内存位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM