I got following problem: Since multiple functions in dictionary.c need access to the hashtable, it should be global. Logical. But my problem is since I want the best possible result (timewise) and design, I don´t know the tablesize, until the load function is called and pass me the dictionary, which I need to examine to determine the optimal tablesize. So I tried to make the table static, but it yells at me that I´m not allowed to make a VLA static or extern. So does anyone have an idea or hint how I can make the table initialized in the load function global? Thx in advance

2 Answers 2


So you need the size to load and you need to load to know the size. This seems infinite.

My suggestion would be using the maximum value that your hash function returns as the size of your hash table indexes and maybe hard-code that value.

  • yeah that sucks XD my hashfunction need the dictionarysize either, and I´m too stubborn to hard-code it, but I think I found a way to declare the hashtable on the heap, and then realloc it in the load-function, i´m still digging...
    – SebBr
    Commented Nov 6, 2014 at 16:24
  • @SebBr I don't think that's a good idea because you'll lose random access to the indexes (i.e., you won't be able to access the linked-list at a specific index using something like hashtable[x] where x is a valid index).
    – kzidane
    Commented Nov 6, 2014 at 16:41
  • How is that? Technically I got a pointer then, don´t I? And I can access it like an array afaik. I´m more concerned about memory leaks though...
    – SebBr
    Commented Nov 6, 2014 at 16:53
  • @SebBr after hashing the word, what are you planning to do exactly?
    – kzidane
    Commented Nov 6, 2014 at 17:39
  • I made it, it´s a lil bit dirty, but works, gonna answer your question in an answer below.
    – SebBr
    Commented Nov 6, 2014 at 20:19

I found a workaround. First I elaborate my approach: I wanted to have an optimal size for the hashtable and I wanted to design it that way that it is optimal for every dictionary that is passed by the load-function so I couldn´t hard-code it.
So my order is:

  1. Determine size of dictionary in loadfunction and call a helperfunction to determine optimal hashtable size(OHS)
  2. Initialize hashtable[OHS]
  3. Call the hashfunction with modulo OHS and attach the nodes to the hashtable

But now the hashtable is only in scope of the loadfunction, and make it static doesn´t work since it´s not allowed for VLAs (variable length array)

The workaround is to declare a global pointer to the array (actually then it´s not an array anymore)

node** hashtable;

and then realloc the hashtable in the load function

hashtable = realloc(hashtable, tablesize * sizeof(node));

The hashtable is accessible like a normal array and available for the other functions. Though since it´s on the heap now, one need to free it manually.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .