Global variables in C ++

So I have something like this

#define HASHSIZE 1010081 static struct nlist *hashtab[HASHSIZE]; 

Now I want to change the HASHSIZE of my hashtab, because I want to check the different numbers of primes and see what gives me less collisions. But arrays do not accept variable sizes, so HASHSIZE must be constant. Is there any way to do this?

+4
source share
6 answers

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

For instance:

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

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

For instance:

  int HASHSIZE=<some_value> static struct nlist *hashtab[HASHSIZE]; 
+10
source

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

A vector works in exactly the same way as an array, but is resizable, and its original size does not have to be a compile-time constant.

 #include <vector> std::vector<nlist*> hash; //empty hash hash.resize(1010081); //now it has 1010081 elementns 
+10
source

Why do you have a global variable for your hash table? Instead, you should probably create a structure or class that can contain the size of the table and a pointer to the table, and dynamically allocate its memory. The next size has a default size, but when creating a hash table, you can pass 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 assume (assuming that you are trying to implement your own hash table and some of your previous questions) that you are interested in learning about the implementation of the hash table at a low level, and therefore I give you a rather low level of answer about how to allocate and free memory on your own. In a real program, using std::vector as described in several other answers would probably be the right decision, as this reduces the amount of accounting you need to do yourself. Again, in a real program, you probably won’t want to implement your own hash table, but instead use an existing table implementation, for example hash_map (not standard, but widely available), boost::unordered_map , or std::tr1::unordered_map (this becomes a standard on the track and is based on boost::unordered_map ).

+4
source

You can use std::vector , as recommended by robson3.14, or allocate an array on the heap with new . If you decide to allocate on the heap, delete []

+1
source

Why don't you just use a constant?

 const int HASHSIZE = 1010081; 

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

0
source

Added variable length arrays to C on C99. However, C99 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 an array on the heap using either std :: vector or the new operator is always preferable for large memory allocations, since the stack space can be limited and stack overrun is a permanent error. Heap allocation can always be checked.

0
source

All Articles