0

This is the code for my load function

bool load(const char *dictionary)
{
    //open dictionary

FILE *inptr = fopen(dictionary, "r");
if (inptr == NULL)
{
    fprintf(stderr, "Could not open %s.\n", dictionary);
    return false;
}

//get the word
char word[LENGTH +1];
while (fscanf(inptr, "%s", word) != EOF)
{
    //load up node
    node *new_entry = malloc(sizeof(node));
    //if we run out of memory
    if (new_entry == NULL)
    {
        free(new_entry);
        return false;
    }
    //else, successful
    strcpy(new_entry -> word, word);

    //hashfunction; a is hastable[0], b is hastable[1]
    int hashtable_index = word[0]-97;

    //link nodes in linked list
    if (hashtable[hashtable_index] -> next == NULL)
    {
        hashtable[hashtable_index] -> next = new_entry;
    }

    else
    {
        new_entry -> next = hashtable[hashtable_index];
        hashtable[hashtable_index] = new_entry;
    }

    free(new_entry);
}

return true;

}

When we load in our first word into our dictionary, my "head" (aka hashtable[hashtable_index]) points to nothing yet. However, I know later on throughout the process, the "head" WILL be pointing to my linked list of nodes. In order to address this, I put an IF statement saying that if hashtable[hashtable_index] -> next is NULL, then the "head" should point to the new entry. If it is not NULL, then I have code that will (hopefully) stitch the new_entry at the front of the linked list, while not losing the "head" pointer to the linked list.

I get segmentation fault for accessing hashtable[hashtable_index] -> next, which is what I expect when no nodes are loaded in yet for "head" to point to. How do go about expressing this IF statement differently?

-1

Without seeing your creation and initialization of hashtable, I can't be sure, but I'm highly confident.

if (hashtable[hashtable_index] -> next == NULL)

If hashtable[hashtable_index] itself is NULL, then ->next doesn't exist. (This is different from "it exists, but is NULL". Because it doesen't exist, the statement above will generate a segfault.

Think of it this way. It's not that the glass of water is empty of water, it's that there is no glass at all!

If this isn't the problem, then we'll need to see the rest of the code.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

6
  • sorry, I didn't copy this in with the code I posted above, but I did initialize a hashtable and node structure: typedef struct node { char word[LENGTH +1]; struct node *next; } node; //hash table node *hashtable[26]; So, the glass exists, its just filled with garbage values at the beginning of the program :) so it can't be the hashtable.... Jul 8 '18 at 1:45
  • Let's extend the metaphor. The hashtable[] is the table that the glass sits on. For hashtable[x] that hasn't been initialized is the broken glass shrads that are sitting on the table. There's no glass! ;-) Same effect. Here's some more detail. If hashtable[x] has NOT been initialized, it indeed contains garbage data.
    – Cliff B
    Jul 8 '18 at 4:48
  • .... The problem is actually worse. If you're lucky, the garbage data will contain the equivalent of an address that isn't allocated to the program. But if you're really, really unlucky, the garbage will contain the equivalent of a random address that actually has been allocated and if the code writes to there, it'll corrupt some memory somewhere in your program without giving you the slightest clue that something is wrong. (unless you're lucky and the OS notices something is amiss and errors it out anyways.) ALWAYS INITIALIZE ARRAYS!
    – Cliff B
    Jul 8 '18 at 4:50
  • by convention, what should i initialize my array of nodes (hashtable) to? Also, I thought by coding in: node *hashtable[26], that is already initializing my array; I just havent assigned specific values to it yet. Jul 8 '18 at 18:26
  • hashtable is an array of pointers. Set them to NULL. In C, only some types of vars are automatically initialized. Pointers are never initialized automatically. Because of this, best practices are to manually initialize all vars every time to assure that it has been done. Also remember that it IS an array of pointers, not an array of structs. The address of the first struct will need to be assigned to this pointer. It's also why the test doesn't work before this first assignment.
    – Cliff B
    Jul 8 '18 at 20:19

You must log in to answer this question.

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