0

I've been stuck on this for hours, please help.

bool check(const char *word)
{
    char check_buffer[LENGTH + 1];

    for(int i = 0; i < strlen(word); i++)
    {
        check_buffer[i] = word[i];
    }
    
    printf("%s\n", check_buffer);
    unsigned int index = hash(word);
    node *cursor = table[index];

    if(strcasecmp(check_buffer, cursor->word) == 0)
    {
        return true;
    }
    else
    {
        while(cursor != NULL)
        {
            printf("%s\n", cursor->word);
            if(strcasecmp(check_buffer, cursor->word) == 0)
            {
                return true;
            }
            cursor = cursor->next;
        }
        return false;
    }

}

Valgrind is reporting that my immense memory leaks are ocurring somewhere in the midst of the check function.

Besides that, whenever I run my code, many words are printed out "printf("%s\n", check_buffer)" from glitched with extra characters on the end: from aaa clinicalcg aardvark databasesg aalesund and aachen registriesg ab B7_ a Use

This looks alot like an "off-by-one" glitch, whereby those characters are garbage values.

So how do I fix this??

1 Answer 1

1

Let's take the easy one first. Think about how check_buffer is populated. The for loop copies every char from word. The problem is that this does NOT copy the end of string marker, \0 to the end of the string! The printf, strlen, and so many other functions depend on this. Without it, printf is going to just keep printing chars until it finds 0x00 as random data in memory. That's where the extra chars come from.

This also fixes a number of "conditional jump or move" errors from valgrind.

However, when I test your check function using the rest of my own code, there were no valgrind memory leaks. I did have to deal with a seg fault in your check code resulting from this (the first use of this line):

if(strcasecmp(check_buffer, cursor->word) == 0)

It is possible, at least in my code, that cursor is null at this point. If an entry in table[ ] is null, it'll cause this to seg fault. Adding a check for that will resolve this issue. Having said that, this first test is unnecessary. The while loop and the code that follows will handle it. You could actually remove everything from this line to the curly brace following the "else" line inclusive, plus the closing curly brace that it pairs with, and the code will work fine, and will be more efficient.

IF you're still seeing valgrind issues after this, please post a new question with full code. It's likely a problem buried somewhere else in the code.

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

3
  • It didn't make my check function work - after running the function I am informed that 99.9% of the words are misspelled. Mar 18, 2021 at 13:48
  • WORDS MISSPELLED: 375902 WORDS IN DICTIONARY: 143091 WORDS IN TEXT: 376904 TIME IN load: 0.02 TIME IN check: 1.20 TIME IN size: 0.00 TIME IN unload: 0.00 TIME IN TOTAL: 1.22 Mar 18, 2021 at 13:48
  • I will be posting the full code Mar 18, 2021 at 13:49

You must log in to answer this question.

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