0

I have been beating my head against a wall trying to solve my issues on my own, but I have gotten no where. I have two issues: 1) I have a small memory leak of 568 bytes in 1 block. My unload function seems to work properly aside from this if the program can get that far, which brings me to 2) I have some form of error occuring with my call for strcmp() saying "Conditional jump or move depends on uninitialised value(s)" and "Uninitialised value was created by a stack allocation". If I run my code with a small dictionary and text file the program will run to completion and return the proper counts for misspelled words, words in dictionary and words in text. The memory leak is the same regardless of sizes. If I try to use the full dictionary with a longer text the program crashes with literally millions of errors. I think that my stack is overflowing, but I cannot figure out why or how to fix it. Below are the chunks of code in question. Please keep in mind, I am very new to computer science so any feedback or pointers to write better code would also be greatly appreciated.

bool check(const char *word)
{
    int b = 0;
   char lword[strlen(word) + 1];
    for (int y = 0; y < strlen(word); y++)
    {
        lword[y] = tolower(word[y]);
    }

    node *checker = head[lword[0] - 'a'];
    while (checker != NULL)
    {
        if (strcmp(lword, checker -> word))
        {
            b = 1;
        }
        checker = checker -> next;
    }
    if (b == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool load(const char *dictionary)
{
    FILE* fp = fopen(dictionary, "r");
    if (fp == NULL)
    {
        return false;
    }

    char term[LENGTH + 1];

    while (fscanf(fp, "%s", term) != EOF)
    {
        counter++;
        node *new_node = malloc(sizeof(node));
        if (new_node == NULL)
        {
            unload();
            return false;
        }
        strcpy(new_node -> word, term);
        int x = term[0] - 'a';
        new_node -> next = head[x];
        head[x] = new_node;
    }
    return true;
}

bool unload(void)
{
    for ( int i = 0; i < 26; i++)
    {
        node *cursor = head[i];

        while (cursor -> next != NULL)
        {
            node *temp = cursor;
            cursor = cursor -> next;
            free(temp);
        }
        free(cursor);
    }
    return true;
}

1 Answer 1

0
  1. 568 bytes is the size of a FILE structure. Using valgrind --show-leak-kinds=all might point you in the same direction.

  2. In check, make sure lword[strlen(word)] is '\0'. Stack variables (and memory allocated on the heap) are not implicitly initialised to zero like global variables are.

Also, I think you got strcmp backwards. It returns 0 (same as false) on equal strings, the sign of a non-zero result would tell you how the two strings relate to each other in a lexicographical order. So whenever strcmp returns 0, you can instantly return true; (or at least break; after b = 1;), skipping the other words in the linked list.

1
  • Thank you very much! I can't believe that I couldn't figure this out myself, but everything is working well now.
    – Ben
    Commented Jul 18, 2018 at 13:53

You must log in to answer this question.

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