I am trying to implement a dictionary of words using a hash table, so I need it to be global, and in one of my header files I declare it
extern node** dictionary;
Where node is
typedef struct node { char* word; struct node* next; } node;
Then in another file in which the functions are defined, I include a header that has a dictionary declaration, and also add at the top
node** dictionary;
Then in the function that actually loads the dictionary, I first allocate memory for linked lists that will make a hash table
bool load(const char* dict_file) { dictionary = malloc(sizeof(node*) * LISTS); FILE* dict = fopen(dict_file, "r"); if(dict == NULL) return false; char buffer[MAX_LEN + 2]; size_dict = 0; while(fgets(buffer, MAX_LEN + 2, dict) != NULL) { node* new_node = malloc(sizeof(node)); int len = strlen(buffer); new_node->word = malloc(sizeof(char) * (len));
So, the program works fine, I free all the allocated memory for lines and nodes, and when I run valgrind (a debugger that detects memory leaks), it says that memory leaks are impossible, but it says that there is an error An uninitialized value was created heap distribution and redirects me to this exact line, where I allocate memory for the dictionary exact first line of the load function that I wrote above. am i doing wrong? I assume that the way I use dictionary globally is wrong, so can anyone suggest another way to keep it global and avoid this error?
source share