I have a problem to free the memory for the variable lowercaseword, I try to do it above the return statement with free(lowercaseword), but then the program stops working and I get errors on check50. Any idea how I should manage to free it and still have a working hash function?
unsigned int hash(const char *word)
{
//Store length of word + place for null character
int wordlength = strlen(word) + 1;
//Assign enought memory in a new variable to store the word
char *lowercaseword = malloc(wordlength);
//Copy the word constant to variable lowercaseword
strcpy(lowercaseword, word);
//Change all characters in lowercaseword to lower case
for (int i = 0; i < strlen(lowercaseword); i++)
{
lowercaseword[i] = tolower(lowercaseword[i]);
}
// djb2 hash function from http://www.cse.yorku.ca/~oz/hash.html
unsigned int hash = 5381;
int c;
while ((c = *lowercaseword++))
{
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
//"Translate" the hash value into the span of the hash-table
hash = hash % N;
return hash;
// TODO
return 0;
}