1

访问 HashMap 类的“put”方法时出现以下错误。我正在使用 VS2008 编译以下代码。

abc.exe 中 0x00eece26 处的未处理异常:0xC0000005:访问冲突读取位置 0xfeeefefe。

在主函数中,我正在创建一个 HashMap 对象的向量。当我尝试调用“HashMap”类的“put”方法时,会出现上述错误。它适用于单个对象,但使用对象向量时它会崩溃。有什么帮助吗?非常感谢。

我正在使用以下类定义

class HashMap 
{
private:
      int TABLE_SIZE;
      LinkedHashEntry **table;

public: 
      HashMap(void){}

      HashMap(int tableSize) 
      {
            TABLE_SIZE = tableSize;
            table = new LinkedHashEntry*[TABLE_SIZE];
            for (int i = 0; i < TABLE_SIZE; i++)
                  table[i] = nullptr;
      }


      double get(int key) 
      {
            int hash = (key % TABLE_SIZE);       
            if (table[hash] == nullptr)
                  return -1;
            else 
            {
                  LinkedHashEntry *entry = table[hash];    
                  while (entry != nullptr && entry->getKey() != key)                     
                        entry = entry->getNext();

                  if (entry == nullptr)
                        return -1;
                  else                    
                        return entry->getValue();
            }
      }


      void put(int key, double value) 
      {
            int hash = (key % TABLE_SIZE);
            if (table[hash] == nullptr)
                  table[hash] = new LinkedHashEntry(key, value);
            else 
            {
                  LinkedHashEntry *entry = table[hash];   
                  while (entry->getNext() != nullptr)
                        entry = entry->getNext();

                  if (entry->getKey() == key)
                        entry->setValue(value);
                  else
                        entry->setNext(new LinkedHashEntry(key, value));
            }
      }     

  // ...
};

LinkedHashEntry 的定义如下。

class LinkedHashEntry 
{
private:
  int key;
  double value;
  LinkedHashEntry *next;

public:
  LinkedHashEntry(int key, double value) {
        this->key   = key;
        this->value = value;
        this->next  = nullptr;
  }

  int getKey() {
        return key;
  }

  double getValue() {
        return value;
  }

  void setValue(double value) {
        this->value = value;
  }

  LinkedHashEntry *getNext() {
        return next;
  }

  void setNext(LinkedHashEntry *next) {
        this->next = next;
  }
};

这是主要方法,我在其中创建向量数组。

#include <vector>

int main()
{
    // ...

    // works fine here
    HashMap objTest(17);
    objTest.put(1,1.1);

    std::vector<HashMap> objHashTable(10, HashMap(17));          
    // crashes here 
    objHashTable[0].put(1, 1.1);

    // ...
}
4

1 回答 1

2

该代码似乎是一个活生生的内存泄漏和维护噩梦。

我无法从发布的代码中观察到它崩溃,但两者

    table = new LinkedHashEntry*[TABLE_SIZE];

    table[hash] = new LinkedHashEntry(key, value);

永远不会被释放。

我建议main使用较少出错/容易出错的类进行重写: http: //en.cppreference.com/w/cpp/container/unordered_map

#include <vector>
#include <unordered_map>

typedef std::unordered_map<int, double> HashMap;

static const int hashTableSize = 17;

int main()
{
    // works fine here
    HashMap objTest(hashTableSize);
    objTest[1] = 1.1; // or objTest.insert({ 1, 1.1 });

    std::vector<HashMap> objHashTable(10, HashMap(hashTableSize));

    // works fine here too
    objHashTable[0].insert({ 1, 1.1 });

    // ZEN achieved; no memory leaks
}

在 Coliru上现场观看

于 2013-08-09T02:55:07.600 回答