简体   繁体   English

Hashmap 使用 nullptr c++ 初始化动态节点数组

[英]Hashmap initializing dynamic array of nodes with nullptr c++

I'm having trouble correctly initializing a dynamic array of nodes with nullptr.我在使用 nullptr 正确初始化节点的动态数组时遇到了问题。

HashMap::HashMap(int size)
{

this->sizeOfArray = size;

this->hashArray = new Node*[this->sizeOfArray];
for (int i = 0; i < this->sizeOfArray; i++)
  {
    hashArray[i] = nullptr;

  }

}

this is what my 'hashArray' looks like in the header.这就是我的“hashArray”在标题中的样子。

Node **hashArray;

the forloop completes all 500 loops but when I am looking at the data in the array, I can only see the first element before I get 'Unable to read memory'. forloop 完成了所有 500 个循环,但是当我查看数组中的数据时,我只能在得到“无法读取内存”之前看到第一个元素。

Here is an image of what I mean https://ibb.co/d0GLw9这是我的意思的图像https://ibb.co/d0GLw9

Here is what Node looks like这是节点的样子

    Node()
{
    value = 0;
}
Node(std::string input)
{
    key = input;
    value = 1;
    next = nullptr;
}
Node(std::string input, int count)
{
    key = input;
    this->value = count;
    next = nullptr;
}
Node(std::string input, int count, Node *next)
{
    key = input;
    this->value = count;
    this->next = next;
}

Node *next;
std::string key;    
unsigned int value; 

I suspect this problem is contributing to me not being able to add any new nodes to the hashArray later on.我怀疑这个问题导致我以后无法向 hashArray 添加任何新节点。

All of your 500 Node pointers are NULL , however the type of the hashArray is Node ** , that's why the debugger shows you only one element as if it's a pointer to one Node pointer.你所有的 500 个Node指针都是NULL ,但是hashArray的类型是Node ** ,这就是为什么调试器只显示一个元素,就好像它是一个指向一个Node指针的指针。 In other words, since your array is dynamic, the debugger doesn't know how many elements to show.换句话说,由于您的数组是动态的,调试器不知道要显示多少个元素。

The error you're getting is in regard to viewing the contents of the first Node whose pointer is NULL , which is naturally can't be read.您遇到的错误与查看第一个指针为NULL Node的内容有关,这自然无法读取。

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

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