0

the program complies properly but when i use valgrind shows am leaking memory when I open the file in the FILE *file = fopen(dictionary, "r");

bool load(const char *dictionary)
{
    // TODO
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        printf("Could not load file\n");
        return 1;
    }
    
    // Create a array space in the memory to hold 45 characters
    char per_word[LENGTH + 1];
    // Loop through every all word int the dictionary with the specified letters
    while(fscanf(file,"%s", per_word) != EOF)
    {
        // Create a new node
        node *n = malloc(sizeof(node));
        
        
        if (n == NULL)
        {
            return false;
        }
        
        // copy the words into the node
        strcpy(n->word, per_word);
        n->next = NULL;
        
        
        
        // HASH THE WORD
        int hashed = hash(per_word);
        // Check if the word is already at the hashed position in table if not set it to n
        if (table[hashed] == NULL)
        {
            table[hashed] = n;
        }
        else
        {
            n->next = table[hashed];
            table[hashed] = n;
        }
        word_count++;
        
    }
    
    return true;
}
0

Close the file at the end of the function. This will prevent memory leakage.

If this helps, please check the tickmark.

You must log in to answer this question.

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