4

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

I declare the array as so in the class:

Node * table;

Then, in another method, I initalize the table:

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

And everything works fine. this->length is a valid entry, this->table is pointing to the right array, and etc. However, then I try 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 bugging out. 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 of "this->table[i] = new Node;").

I hate to post long segments of code, so here's 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 I'm doing is telling me what the error is; any help is appreciated!

4 Answers 4

4

You don't have an array of pointers. You have an array of Nodes. Apparently, what you want is something like this:

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

Or maybe you don't actually need an array of pointers, but simply an array of nodes. In that case, no extra initialization is needed beyond:

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

Beyond that, unless this is a learning exercise, take a look at the standard library, which has dynamic arrays and hash maps all ready for you.

Sign up to request clarification or add additional context in comments.

Comments

1

table is not an array of pointers. It's an array of Nodes (or rather, it points to an array of Nodes). The type of table is Node*; the type of table[i] is Node, not Node*.

If you actually do want an array of pointers, then you need

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

Or better still, something like

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

Comments

0

You do not have declared an array of pointers.
Node *table(point to a node)
Node **table(point to an array Nodes)

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

Comments

0
this->table = new Node [HashMap::length];

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

this->table[i] = new Node;

As an example, we 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 new Node returns Node*. Hence incompatible types. Correct line would be

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

But, this line is unnecessary as the array of Nodes is already created and the memory is allocated. Using this line in the code will lead to a memory leak.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.