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
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:
- Determine size of dictionary in loadfunction and call a helperfunction to determine optimal hashtable size(OHS)
- Initialize hashtable[OHS]
- 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)
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.