0

I'm working on speller and am having problems with my check function. I thought that my load function is working right, I'm not getting any errors or memory leaks. But, when I look in my hashtable array where the first word of each letter should be, the addresses are all the same. Meaning that if I load a dictionary with two words, "animal" and "bear", the entry for 'a' and 'b' (hashtable[0] and hashtable[1]) will both have a memory address of 0x603250. So then, when I send my cursor to the beginning of a linked list to search for the right word, cursor->next just goes in a circle and never reaches null, so I'm stuck in an infinite loop.

Any suggestions?

typedef struct node {
    char word[LENGTH + 1];
    struct node *next;
}
node;

node *hashtable[26] = {0};
char word[LENGTH];



//sends my cursor to the right linked list to search for a word
int hash(const char *word) {
if (word[0] >= 'A' && word[0] <= 'Z') {
    int key = word[0] - 65;
    return key;
} 

else {
    int key = word[0] - 97;
    return key;
}


}

/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
    char *holder = malloc(sizeof(word) +1 );

strcpy(holder, word);

int reset = 0;

for (int i = 0; i < strlen(word); i++) {
    if ( !((holder[i] >= 'z' && holder[i] <= 'z' ) || holder[i] == '\'') ) {
        if ( !((holder[i]) == '\'') ) {
            *holder = tolower(*holder);
            holder++;
            reset++;
        }

        else {

        }
    }

    else {

    }
}

holder = holder - reset;
node *cursor = hashtable[hash(word)];

while (cursor != NULL) {

    if (strcmp(holder, cursor->word) == 0) {
        free(holder);
        free(cursor->word);
        free(cursor);
        return true;
    }

    else {
        cursor = cursor->next;
    }
}

free(holder);
free(cursor->word);
free(cursor);

return false;
}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{
int wordCount = 0;

FILE *text = fopen(dictionary, "r");

if (text == NULL) {
    printf("Could not load your dictionary, please try again\n");
    return false;
}


while (fscanf(text, "%s", word) != EOF) {
    node *newNode = malloc(sizeof(node));
    int hashHolder = 0;
    if (newNode == NULL) {
        unload();
        return false;
    }

    strcpy(newNode->word, word);

    hashHolder = newNode->word[0] - 97;

    if (hashtable[hashHolder] == NULL) {
        hashtable[hashHolder] = newNode;
        newNode->next = NULL;
        wordCount++;
    }

    else {
        newNode->next = hashtable[hashHolder];
        hashtable[hashHolder] = newNode;
        wordCount++;
    }

    //free(newNode->next);
    free(newNode);
}



fclose(text);
return true;
}

thanks so much!

1 Answer 1

1

The proximate problem is free(newNode);. You are building this hashtable with addresses to memory that need to persist for the life of the program. That's the essential point of a separate unload function. Program is assigning the same address to hashtable[1] because you just told it to free the memory at address 0x603250.

You will probably run into problems in check too, with free(cursor) and free(cursor->word) because, well, you need those nodes to persist.

Looks like other troubles brewing in the check function, but once you have a good dictionary loaded, debug50 will help you to the finish!

1
  • Great! Deleting the free() solved my problems for the time being. Thanks! Commented Jun 1, 2017 at 16:36

You must log in to answer this question.

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