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.