0

I recently made the speller excercise and it is working, has no memory leaks but I am getting some Invalid read and Invalid write errors from Valgrind in my check function. First I got a error because of uninitialized values of which I got around with calloc but still the read and write errors and I do not really know how to get around. The lines that Valgrind are telling me are marked.

bool check(const char *word)
{
    // TODO
    //unsigned long hashes = hash(word);
    node *ptr = table[hash(word)];

    // Do I need to put sizeof(word) + 1 or is sizeof(string) giving me enough
    // space for the null character?
    char *placeholder = calloc(1, sizeof(word) + 1);
    int counter = 0;
    int counter_length = 0;
// Here I get an error
    for(const char* s = word; *s != '\0'; s++)
    {
// Here I get an error
        placeholder[counter] = tolower(word[counter]);
        counter++;
    }
    placeholder[counter] = '\0';
    counter = 0;


    while(ptr != NULL)
    {
        // I made it so cumbersome because I wanted to find out why
        // Valgrind is getting me the following errors on this part:
        // "Invalid write of size 1" and
        // "Ivalid read of size 1"
// Here I get an error
        counter_length = strlen(placeholder);
// Here I get an error
        for(int i = 0; i < counter_length; i++)
        {
            if(placeholder[i] == ptr->word[i])
            {
                counter++;
            }
        }
        if(counter_length == counter)
        {
            free(placeholder);
            return true;

        }

       /* This was my first not so cumberstone way of checking if the strings are same
       if (strcmp (placeholder, ptr->word) == 0)
        {
            free(placeholder);
            return true;

        }*/
        ptr = ptr->next;
        counter = 0;
    }
    free(placeholder);
    return false;
}

I am quite new to computer science so I would be happy if you have any other hints on what I could do better and why. Thanks in advance.

1 Answer 1

0

RE:

// Do I need to put sizeof(word) + 1 or is sizeof(string) giving me enough // space for the null character?

Neither. Are you confusing sizeof with strlen? word is a pointer, sizeof(word) is 8 on the IDE, regardless of the length of the word.

3
  • AHH, yes your are right. I changed that line to: char *placeholder = calloc(1, sizeof(strlen(word)) + 1); but I am still getting the invalid write and reads
    – Un1kum
    Commented Sep 6, 2021 at 20:52
  • After recompile?????? My results using this check are different than yours 1) it undercounts misspelled words (difference in hash perhaps; tested constitution.txt with large dictionary) and 2) it doesn't give valgrind errors. Sorry. Commented Sep 7, 2021 at 13:17
  • Yeah it is undercounting with the cumberstone way of comparing the two string which i implemented to see why I get the invalid writes and reads but working correctly with strcmp but on both ways I get the invalid reads and writes. So when you are not getting the errors then I think the error is not in check as valgrind is supposing but in some other part of the programm. At least I have found out that and will take a closer look at the other parts of the programm.
    – Un1kum
    Commented Sep 7, 2021 at 19:39

You must log in to answer this question.

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