Cannot initialize array of pointers in C ++

I have an array of pointers to a structure called a "table" (a structure called Node).

I declare an array as such in the class:

Node * table; 

Then, in another method, I initialize the table:

 this->table = new Node [this->length]; 

And everything works fine. this-> length is a valid entry, this-> table points to the correct array, etc. However, I am trying to change the value of the elements:

 for(int i = 0; i < this->length; i++) { this->table[i] = new Node; } 

Or even

 for(int i = 0; i < this->length; i++) { this->table[i] = 0; } 

And everything starts to bugger. Why can't I set these pointers to anything?

This is the error I get: enter image description here

(where line 15 is the line "this-> table [i] = new Node;").

I don’t like to place long code segments, so here is a shortened version of the code itself:

 template <class T, class S> class HashMap { public: HashMap(); private: struct Node; Node * table; static const unsigned length = 100; }; template <class T, class S> HashMap<T, S>::HashMap() { this->table = new Node [HashMap::length]; for(int i = 0; i < HashMap::length; i++) { this->table[i] = new Node; } } template <class T, class S> struct HashMap<T, S>::Node { T value; S key; Node * next; }; 

No research that I do tells me what error is; any help is appreciated!

+4
source share
4 answers

You do not have an array of pointers. You have an array of nodes. Apparently something like this:

 Node ** table; ... this->table = new Node*[this->length]; 

Or maybe you don't need an array of pointers, but just an array of nodes. In this case, additional initialization is not required:

 this->table = new Node[this->length]; 

In addition, if this is not a training exercise, take a look at the standard library that has dynamic arrays and hash maps all ready for you.

+4
source

table not an array of pointers. This is a Node array (or rather, it points to an array from Node s). Type table - Node* ; type table[i] is Node , not Node* .

If you really need an array of pointers, you need to

 Node** table; table = new Node*[length]; 

Or better yet, something like

 vector<unique_ptr<Node>> table; table.resize(length); 
+1
source

You did not specify an array of pointers.
Node * table (point to node)
Node ** table (point to the nodes of the array)

 Node** table; table =new Node*[this->length]; for(int i=0;i<this->length;i++) { table[i]=new Node; } 
0
source
 this->table = new Node [HashMap::length]; 

this->table is of type Node* , and the new Node [HashMap :: length] also returns Node *, ie an array is created from the Node of length HashMap::length and the address of the array is stored in this->table pointer.

 this->table[i] = new Node; 

As an example, you can define:

 int* arr = new int[10]; 

Here arr is of type int *, but arr[0] will be of type int.

similarly, this->table[i] is of type Node, and the new Node returns Node *. Therefore, incompatible types. The correct line will be

 this->table[i] = *new Node; 

But this line is not needed, since an array of nodes has already been created and memory is allocated. Using this line in code will result in a memory leak.

0
source

Source: https://habr.com/ru/post/1496955/


All Articles