1

I was reading an example of a hash table implementation in C++ from a website and saw this.

private:
  HashEntry **table;
public:
  HashMap() {
        table = new HashEntry*[TABLE_SIZE];
        for (int i = 0; i < TABLE_SIZE; i++)
              table[i] = NULL;
  }

The line with the syntax I don't understand is:

table = new HashEntry*[TABLE_SIZE];

What does it mean to have the asterisk before the brackets like that?

James4701
  • 63
  • 1
  • 7

2 Answers2

5

new HashEntry*[TABLE_SIZE] allocates and constructs an array of TABLE_SIZE elements, where each element is a HashEntry*, i.e. a pointer to a HashEntry.

A more modern C++ version of that would be:

private:
  std::vector<std::unique_ptr<HashEntry>> table;
public:
  HashMap() : table(TABLE_SIZE) {}

This avoids having to define your own destructor, and is generally safer.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436