So I am currently working on Pset4 speller and implementing it using hastable. I have written the load() function but I am not sure if it's correct as you need to compile speller.c and not dictionary.c, is there a way to know whether the function works as I would like to be sure that my load() function works before proceeding ahead.

// Buffer for a word
char word[LENGTH + 1];
int x;
// Insert words into hash table
while (fscanf(file, "%s", word) != EOF)
    //Create a newnode
    node *newnode = malloc(sizeof(node));
    if(newnode == NULL)
        return false;
    //Copy the word into newnode
    //Use the hash function to check correct postion in hastable
    x = hash(newnode->word);
    //Add newnode to the hastable
    newnode->next = hashtable[x];
    hashtable[x] = newnode;


That snippet on itself looks fine to me.

I guess your code is pretty readable, as vulnerable to buffer overflows (by a word too long for the buffer, which won't be tested for) as most other solutions here, it has comments, variable names are not super short (apart from x) or strange/meaningless (again x). Don't forget the style50 before submitting.

You won't know whether it is working until you have other parts ready, and even then you can't test them properly on their own. Partially, this is due to the checker leaving all the implementation details to you, so it can't substitute a part of the implementation with its own. Memory issues (which you'll find in many flavours around malloc/free) are often a bit hard to trace, there are a few common patterns of where certain failures originate, but some people even have messed up their hash table within check (which is not meant to change the data structure at all). Make sure your algorithm makes sense and your implementation matches the algorithm, the less bugs in the code, the less you have to hunt later.

  • 1
    You can also create additional functions or temporarily insert pieces of code to check various things. For instance, you could create a function to print out the entire tree/trie to verify its correctness. Just do so with a small dictionary that's tailored to test the load function. You don't want to print out the full 140k word dictionary!
    – Cliff B
    Apr 25 '19 at 17:32
  • @CliffB That's true, and for a small (crafted) dict and a hash table, this is feasible, for a trie it would be a more complex task, potentially introducing errors in the code meant to test for correctness.
    – Blauelf
    Apr 29 '19 at 9:39
  • All true, @blauelf. Any code could potentially introduce errors. But that's part of the game. It's also why the extra test code should be written as independent functions and not incorporated into the existing functions whenever possible. It's also a skill that needs to be developed and polished, especially important while learning to program.
    – Cliff B
    Apr 29 '19 at 20:38

You must log in to answer this question.

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