I know this is one of the most frequently asked questions here, since I' ve spent more than an hour reading posts by people that faced similar problems. But still, I can't tell what's going on in my situation.
What I know:
- My code compiles locally
- Program runs as expected (when compared with the solutions given by the cs50 staff)
- Valgrind shows no memory related errors
- I didn't change anything in speller.c and/or dictionary.h. I only wrote the functions in the dictionary.c file.
- Since I read the posts of many people that faced similar issues with this pset, I realized that at some point check50 was broken, but the comments stating this are more than 1 year old by now, so I highly doubt it they haven't fixed that yet.
What I DON'T know:
- Why check50 fails to compile my code even when I check with check50 locally (check50 cs50/problems/2024/x/speller -l) (expected exit code 0, not 2)
- Since my code can't compile (according to check50), I don't even know if it passes all the tests, because every single one of them says "can't check until a frown turns upside down".
Here is my code:
// Implements a dictionary's functionality #include <ctype.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include "dictionary.h" // Represents a node in a hash table typedef struct node { char word[LENGTH + 1]; struct node *next; } node; // TODO: Choose number of buckets in hash table (Additional comment: each bucket is a linked list) const unsigned int N = 4500; // Hash table node *table[N]; int words_amount = 0; bool is_loaded = false; // Returns true if word is in dictionary, else false bool check(const char *word) { // TODO int bucket_indx = 0; char *lowercase_word = malloc((sizeof(char)) * (strlen(word) + 1)); strcpy(lowercase_word, word); for (int i = 0; lowercase_word[i] != '\0'; i++) { lowercase_word[i] = tolower(lowercase_word[i]); } bucket_indx = hash(lowercase_word); node *cursor = table[bucket_indx]; while (cursor != NULL) { if (strcasecmp(cursor->word, lowercase_word) == 0) { free(lowercase_word); return true; } else { cursor = cursor->next; } } free(lowercase_word); return false; } // Hashes word to a number unsigned int hash(const char *word) { // TODO: Improve this hash function int hash_addition = 0; char *lowercase_word = malloc(strlen(word) + 1); if (lowercase_word == NULL) { return false; } strcpy(lowercase_word, word); for (int i = 0; lowercase_word[i] != '\0'; i++) { lowercase_word[i] = tolower(lowercase_word[i]); } int length = strlen(lowercase_word); for (int i = 0; i < length; i++) { hash_addition = hash_addition + (lowercase_word[length - i] * lowercase_word[i]); } free(lowercase_word); return hash_addition % N; } // Loads dictionary into memory, returning true if successful, else false bool load(const char *dictionary) { // TODO char buffer[LENGTH + 1]; node *n = NULL; // Open the dictionary file FILE *source = fopen(dictionary, "r"); if (source == NULL) { printf("Error: Could not open file.\n"); return false; } // Copy each word read from the file to a new node else { while (fscanf(source, "%s", buffer) != EOF) { n = malloc(sizeof(node)); { if (n == NULL) { fclose(source); return false; } } // Convert word in buffer to lowercase, character by character for (int i = 0; buffer[i] != '\0'; i++) { buffer[i] = tolower(buffer[i]); } strcpy(n->word, buffer); int index = hash(buffer); n->next = table[index]; table[index] = n; words_amount++; } is_loaded = true; fclose(source); return true; } } // Returns number of words in dictionary if loaded, else 0 if not yet loaded unsigned int size(void) { // TODO if (!is_loaded) { return 0; } else { return words_amount; } } // Unloads dictionary from memory, returning true if successful, else false bool unload(void) { // TODO int i = 0; node *cursor = NULL; node *tmp = NULL; for (i = 0; i < N; i++) { cursor = table[i]; while (cursor != NULL) { tmp = cursor; cursor = cursor->next; free(tmp); } } // At this point cursor should be NULL anyway, so checking if i= N is enough to find out if all // the nodes are freed if (i == N) { return true; } else { return false; } } Thanks in advance to anyone willing to help me!



hashand look at the type of the variable you are storing it into. Also you are not assigning a NULL when you do your strcpy's to the last character so you are going to run into problems eventually. thats a bug.dictionary.cinto my codespace, (including fresh download of the distro), compiled it (success) and rancheck50, no errors reported (locally and not).