0

I am currently working on the load function in Speller. When I run it with a file, most of the words are mispelled, leading me to believe that initializing my list works correctly, until it adds another pointer to the linked list. Am I doing this correctly?

bool load(const char *dictionary){
    

    //open file
    FILE *file = fopen(dictionary, "r");
    
    //initialize a holder
    
    char buffer[LENGTH + 1];
    node *m;

    //while loop
    while (fgets(buffer, LENGTH + 1, file) != NULL){
        //make node

        m = malloc(sizeof(node));
        if (m == NULL){
            return false;
        }
        
        
        
        //copy node
        strcpy(m -> word, buffer);
     
        //set next pointer to NULL
        m -> next = NULL;

        //hash the word
        int h = hash(m->word);
        node *cursor = table[h];
        
        //if table location has list started
        if (table[h] != NULL){
            while (cursor != NULL){
                //printf("%s", cursor->word);
                cursor = cursor -> next;
            }
            
            cursor = m;

        }
        //start list
        else{
            table[h] = m;
        }
       
        //count words in dictionary and free malloc
        word_size++;
        free(m);
        

    
    }
    fclose(file);

    
    return true;

This is the error I get when I run it through cat.txt and the small dictionary.

MISSPELLED WORDS

Word: (null) : A
Word: (null) : cat
Word: (null) : is
Word: (null) : not
Word: (null) : a
Word: (null) : caterpillar

WORDS MISSPELLED:     6
WORDS IN DICTIONARY:  2
WORDS IN TEXT:        6
TIME IN load:         0.00
TIME IN check:        0.00
TIME IN size:         0.00
TIME IN unload:       0.00
TIME IN TOTAL:        0.00


And this is the error with the large dictionary. 
WORDS MISSPELLED:     17692
WORDS IN DICTIONARY:  143092
WORDS IN TEXT:        17756
TIME IN load:         0.02
TIME IN check:        0.01
TIME IN size:         0.00
TIME IN unload:       0.00
TIME IN TOTAL:        0.03

Thank you in advance!

0

You've made a mistake that is very common while working with pointers. You're calling free() on memory being pointed to by table[]. When you do table[h] = m, you're not making a copy of the memory being pointed to by m, you're just copying the pointer to the memory. You have to keep the memory until you free it in unload().

Also, your line cursor = m; is a subtle logic error. That just points the variable cursor at m. You need to assign to the next field of the object being pointed to by cursor, which you can do with cursor->next = m;

The rest of your code seems correct.

If this answers your question, please click on the check mark to accept. Don't be shy to ask another question the next time you have an issue!

  • Thank you so much for your reply! I did correct my code based on your suggestions, but however have a valgrind error with storage. If I don't free(m), how do I get rid of the error? – aidan Sep 2 '20 at 18:22

You must log in to answer this question.

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