0

My code is now working fine but misses out on fully completing Check50 because of the last requirement. Running ~/pset5/speller/ $ help50 valgrind ./speller texts/cat.txt, it returns:

==6259== Memcheck, a memory error detector
==6259== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6259== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==6259== Command: ./speller texts/cat.txt
==6259== 

MISSPELLED WORDS


WORDS MISSPELLED:     0
WORDS IN DICTIONARY:  143091
WORDS IN TEXT:        6
TIME IN load:         1.70
TIME IN check:        0.01
TIME IN size:         0.00
TIME IN unload:       0.22
TIME IN TOTAL:        1.92

==6259== 
==6259== HEAP SUMMARY:
==6259==     in use at exit: 552 bytes in 1 blocks
==6259==   total heap usage: 143,096 allocs, 143,095 frees, 8,023,416 bytes allocated
==6259== 
==6259== 552 bytes in 1 blocks are still reachable in loss record 1 of 1
==6259==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==6259==    by 0x5258E49: __fopen_internal (iofopen.c:65)
==6259==    by 0x5258E49: fopen@@GLIBC_2.2.5 (iofopen.c:89)
==6259==    by 0x4011D1: load (dictionary.c:77)
==6259==    by 0x4009B4: main (speller.c:40)
==6259== 
==6259== LEAK SUMMARY:
==6259==    definitely lost: 0 bytes in 0 blocks
==6259==    indirectly lost: 0 bytes in 0 blocks
==6259==      possibly lost: 0 bytes in 0 blocks
==6259==    still reachable: 552 bytes in 1 blocks
==6259==         suppressed: 0 bytes in 0 blocks
==6259== 
==6259== For counts of detected and suppressed errors, rerun with: -v
==6259== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Asking for help...

Sorry, help50 does not yet know how to help with this!

I have identified that the problem lies within my load function, so I've attached that below. Also, comparing the total number of allocs to the total number of frees, I may be missing one free somewhere. Any help would be appreciated!

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary){

//Open dictionary file//
FILE *file = fopen(dictionary, "r");

//Check if file loaded is empty
if(file == NULL)
{
    printf("Could not load dictionary.\n");
    return false;
}

//Character aray of max size 45 + 1
char buffer[46];

//Read strings from file one at a time//
while (fscanf(file, "%s", buffer) != EOF)
{
     //Create a new node for each word//
    node *new_word = malloc(sizeof(node)); //Allocate memory for a node pointer

    //Initialize values of node new_word
    new_word->next = NULL;

    //Check if you have enough memory to malloc
    if(new_word == NULL)
    {
        printf("Not enough memory.\n");
        return false;
    }

    //Copy word into node
    strcpy(new_word->word, buffer);

    //Hash word to obtain hash value//
    int hash_value = hash((const char *)buffer);

    //Insert node at table[hash_value]//
    if(table[hash_value] == NULL)
    {
        table[hash_value] = new_word;
    }
    else
    {
        new_word->next = table[hash_value];
        table[hash_value] = new_word;
    }

    word_counter++;
}

if(feof(file))
{
    return true;
    fclose(file);
}

return false;
}
1

The biggest clue is in the valgrind output:

==6259==    by 0x5258E49: fopen@@GLIBC_2.2.5 (iofopen.c:89)
==6259==    by 0x4011D1: load (dictionary.c:77)

This tells you where the allocation of the memory is located, but not where the problem lies. So, let's look at line 77.

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

That tells me that the problem isn't really a malloc, it's an open file that's not being closed. (fopen is a distant cousin of malloc. It does allocate memory to a file pointer.) So, where is the file pointer closed?

if(feof(file))
{
    return true;
    fclose(file);
}

Question: What happens when a return statement is executed? What's the next line of code to be executed????? ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Haha a really simple mistake! Thanks for the clarification! – lmcc Jun 20 '20 at 0:19

You must log in to answer this question.

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