0

I'm working on pset5 2020 speller. I have managed to get my code to compile but no matter how small the input data sets are I suffer a segmentation fault. I've run valgrind and there only seems to be one issue. However, I find myself unable to figure out how to solve it on my own. I'm hoping that, once I get rid of the segfault, I'll be able to isolate and fix the inevitable other issues in my code on my own, butI would greatlly apprciate it if somjeone could help me out here and either tell me how to interpret the error messages, what I'm doing wrong or possibly a good troubleshooting method for these types of issues, so I may figure out the issue on my own accord. Any and all forms of aid will be greatly appreciated!!!

What I believe to be the relevant error messages are:

> LEAK SUMMARY:
> ==3904==    definitely lost: 392 bytes in 7 blocks //(unload function)
> ==3904==    indirectly lost: 0 bytes in 0 blocks
> ==3904==      possibly lost: 0 bytes in 0 blocks
> ==3904==    still reachable: 3,240 bytes in 49 blocks //(load + possibly check?)
> ==3904==         suppressed: 0 bytes in 0 blocks

These are my load and unload functions:

bool check(const char *word)
{
    //Declarations
    bool found = false;
    int check_hash = hash(word);
    node *cursor = malloc(sizeof(node));
    if(cursor == NULL)
    {
        printf("Memory Error.\n");
        return false;
    }
    cursor->next = table[check_hash]->next;

    while(cursor->next != NULL)
    {
        if(strcasecmp(cursor->word, word))
        {
            found = true;
        }
        cursor = cursor->next;
    }
    free(cursor);
    return found;
}
bool load(const char *dictionary)
{
    FILE *dic = fopen(dictionary, "r");
    if (dic == NULL)
    {
        printf("Could not load %s\n", dictionary);
        fclose(dic);
        return false;
    }
    char* nword = malloc(sizeof(char)*(LENGTH + 1));

    while(fscanf(dic,"%s",nword) != EOF)
    {

        fscanf(dic,"%s",nword);
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            printf("Could not allocate memory.\n");
            fclose(dic);
            free(n);
            return false;
        }
        strcpy(n->word, nword);
        word_count++;
        n->next = NULL;
        int index = hash(n->word);

        n->next = table[index];

        table[index] = n;

    }
    free(nword);
    return true;
}
bool unload(void)
{
    //Declarations
    int freed = 0;
    node *cursor = malloc(sizeof(node));
    if (cursor == NULL)
    {
        printf("Memory Error: Unloading.");
        free(cursor);
        return false;
    }

    node *tmp = malloc(sizeof(node));
    if (tmp == NULL)
    {
        printf("Memory Error: Unloading.");
        free(tmp);
        return false;
    }


    //Function
    for (int i = N; i > 0; i--)
    {
        cursor->next = table[i];
        while(cursor->next != NULL)
        {
            tmp = cursor;
            cursor = cursor->next;
            free(tmp);
        }
        freed++;
    }
    free(cursor);
    if (freed == N)
    {
        return true;
    }
    else
    {
        return false;
    }
}

If this is somehow a double of another question, it would be wonderful if someone could point me in the right direction!:) :)

0

char* word = malloc(sizeof(LENGTH + 1)); is one problem. LENGTH + 1 is an int, sizeof(_int_) is 4 (in the IDE).

A little further on free(word) is inside the while loop. The allocation is outside the while loop. What happens on the 2nd fscanf?

But wait, there's more!

table is (presumably) an array of node pointers, not an array of nodes, therefore there is no "next" member (table[index]->next = n;)

10
  • Thank you! I have fixed the first two things you mentioned. I am unsure of what you mean by your comment on the second fscanf. I assumed it would need to be there so the loop will iterate over the function call. Is that not the case? Does it already scan in the string when it checks the condition of the loop?
    – Renée
    May 12 '20 at 15:09
  • However, now that I have placed free(word) outside of the loop and corrected my malloc call I am losing more bytes(still in the same manner as in the error messages above just now there is an extra "46 bytes in 1 blocks are still reachable" before the other two messages.
    – Renée
    May 12 '20 at 15:17
  • added another problem area to the answer. May 12 '20 at 16:13
  • I have attempted to take the ->next away after the table[index], but that results in a huge memory leak. 392 bytes in 7 blocks definetly lost AND 4'006'848 bytes in 71'542 blocks still reachable. I'm sorry to keep bothering you with this, by the way, I really hope I'm not annoying too much with my incompetence.
    – Renée
    May 13 '20 at 7:19
  • It's not incompetence, it's learning. Still getting segfault? Make sure program does not try to access hash[i]->next anywhere in the program (even it it's called by a different name, hint hint unload). May 13 '20 at 16:25

You must log in to answer this question.

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