tags:

views:

248

answers:

6

So I have something like this

#define HASHSIZE 1010081

static struct nlist *hashtab[HASHSIZE];

Now I want to be able to change the HASHSIZE of my hashtab, because I want to test different primes numbers and see which would give me less collisions. But Arrays do not take variable sizes so HASHSIZE has to be a constant. Is there a way to go about this?

A: 

Why don't you just use a constant?

const int HASHSIZE = 1010081;

A constant is available to the compiler as a literal value so it can be used to initialize arrays (amoung other things).

Andrew Hare
I will be changing its value, so const wont work?
SuperString
However, it is better than using `#define`.
David Thornley
+10  A: 

use std::vector, described in any good book on C++.

Vector works just like an array but is resizable, also its initial size doesn't have to be a compile-time constant.

#include <vector>

std::vector<nlist*> hash; //empty hash
hash.resize(1010081); //now it has 1010081 elementns
robson3.14
+14  A: 

Why don't you use std::vector instead of using arrays in C++?

Eg:

  std::vector<nlist *> hashtab; 
  hashtab.resize(<some_value>); 

But anyways you can do this if you are using g++ because g++ supports Variable Length Arrays(VLAs) as an extension.

Eg:

  int HASHSIZE=<some_value>
  static struct nlist *hashtab[HASHSIZE];
Prasoon Saurav
+1  A: 

You can either use std::vector as recommended by robson3.14 or allocate the array on the heap with new. If you choose to allocate on the heap be sure to delete []

Jesse
+4  A: 

Why do you have a global variable for your hash table? Instead, you should probably create a structure or class, which can contain the size of the table and a pointer to the table, and allocate its memory dynamically. The following has a default size, but you can pass in a different size when creating the hash table to try out different sizes.

class HashTable {
public:
  HashTable(int size = 1010081) : m_size(size) {
    m_table = new nlist *[m_size];
  }
  ~HashTable() {
    delete[] m_table;
  }

  // Define getters, setters, etc. here...

private:
  int m_size;
  struct nlist **m_table;
};

note: I'm assuming (based on the fact that you're trying to implement your own hash table, and some of your previous questions) that you are interested in learning about the low-level implementation of a hash table, and thus I'm giving you a fairly low-level answer about how to allocate and deallocate the memory yourself. In a real-world program, using std::vector, as described by several of the other answers, would probably be the right thing to do, as it reduces the amount of bookkeeping you need to do yourself. Then again, in a real-world program, you probably wouldn't want to implement your own hash table, but instead use an existing has table implementation like hash_map (not standard, but widely available), boost::unordered_map, or std::tr1::unordered_map (this is on the track to becoming a standard, and based on boost::unordered_map).

Brian Campbell
A: 

Variable length arrays have been added to C in C99. C99 however is not included in the C++ standard. GCC 4.2.2 allows the use of variable length arrays in C++ (here)

Having said that, allocating the array on the heap using either a std::vector or the new operator is always preferable for large memory allocations since stack space may be limited and overrunning the stack is an irrecoverable error. Heap allocations can always be checked.

doron