However revised, this is a repost of a now deleted earlier question describing a slightly different issue.
My load()
function updates a global array initialized to NULL
. check()
thereafter converts word
to lowercase and stores it in a buffer. Most elements in the array store NULL
, however sometimes a real memory address is found and the condition for the while
loop, as well as the if
condition succeeds and true;
gets returned.
The relevant two functions are below together with global variables and arrays.
// Global variables and arrays for simple access
char buffer[LENGTH+1]; // Declares the buffer to store one line or one word in total
int wordcount = 0;
int hash_index;
node* head_array[HASHTABLE_SIZE] = {NULL}; // Stores pointer to node datatype
node* head_node; // Will always be the most recent node, and first in the list
FILE* dict_file;
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
for (int i = 0, wordlen = strlen(word); i < wordlen; i++) {
buffer[i] = tolower(word[i]);
buffer[i+1] = '\0';
}
hash_index = hash(buffer);
head_node = head_array[hash_index];
while (head_node != NULL) {
if (strcmp(buffer, head_node->word))
return true;
head_node = head_node->next;
}
return false;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
// Open dictionary file, and error-check
dict_file = fopen(dictionary, "r");
if (dict_file == NULL)
return false;
while (fgets(buffer, LENGTH+1, dict_file) != NULL) {
// Temporary reference to the current hash
hash_index = hash(buffer);
// Create new node and fill it's word field with the contents of buffer
head_node = malloc(sizeof(node));
if (head_node == NULL) {
printf("malloc returned NULL");
return false;
}
strcpy(head_node->word, buffer);
if (head_array[hash_index] == NULL)
head_node->next = NULL;
else
head_node->next = head_array[hash_index];
head_array[hash_index] = head_node;
wordcount++;
}
return true;
}
Some messing around with gdb says that head_array
contains:
{0x0, 0x0, 0xebd750, 0x0, 0x0, 0x0, 0xebef10, 0x0, 0x0, 0x0, 0xebe510,
0x0, 0x0, 0x0, 0xebed10, 0x0, 0x0, 0x0, 0xebee50, 0x0, 0x0, 0x0, 0xebded0,
0x0, 0x0, 0x0, 0xebe890, 0x0, 0x0, 0x0, 0xebeb50, 0x0, 0x0, 0x0, 0xebe550,
0x0, 0x0, 0x0, 0xebe650, 0x0, 0x0, 0x0, 0xebebd0, 0x0, 0x0, 0x0, 0xebecd0,
0x0, 0x0, 0x0, 0xebe850, 0x0, 0x0, 0x0, 0xebed50, 0x0, 0x0, 0x0, 0xebeed0,
0x0, 0x0, 0x0, 0xebe110, 0x0, 0x0, 0x0, 0xebee90, 0x0, 0x0, 0x0, 0xebec10,
0x0, 0x0, 0x0, 0xebee10, 0x0, 0x0, 0x0, 0xebe410, 0x0, 0x0, 0x0, 0xebe1d0,
0x0, 0x0, 0x0, 0xebe610, 0x0, 0x0, 0x0, 0xebe010, 0xbd1a90, 0x0, 0x0,
0xebedd0, 0x0, 0x0, 0x0, 0xebec50, 0x0}