0

Changing this question to be specific. My load function seems to run past the end of the cs50 small dictionary to give a segmentation fault after it works for cat and caterpillar. I can see that cat links to caterpillar, but then the while loop executes again for some reason. Code updated.

Would appreciate any help as to why it would do this?

Code below Global code:

 // define the node structure
typedef struct node
{
char word [LENGTH + 1];
struct node* next;
}
node;

// initialise a word counter
int counter;

// create hash table array
node* hashtable[27];

// create a hash function to use
int hash_function(char* word)
{
   // hash first letter of string
   int hash = tolower(word[0]) - 97;
   return hash % 27;
}

Code to be written inside the load function:

bool load(const char* dictionary)
{

 // open dictionary file to read and check if NULL
 FILE* dict = fopen(dictionary, "r");
    if (dict == NULL)
     {
        printf("cannot %s\n", dictionary);
        return false;
     }

    for (int i = 0; i < 27; i++)
        {
            hashtable[i] = NULL;
        }  while(!feof(dict))

    // do this next code until the end of the dictionary is reached
     while(!feof(dict))
     {
      // create a new node
      node* new_node = malloc(sizeof(node));

     // scan the dictionary for words to make nodes to point to them
     fscanf(dict, "%s", new_node->word);

     // hash the new word to find where it goes in array
      int hash = hash_function(new_node->word);

    // If the hash table index has nothing in it, put the word in
    if (hashtable[hash] == NULL)
     {
      hashtable[hash] = new_node;
      new_node->next = NULL;
     }
  else
      {
     // make a temp node 
     node* temp_node = hashtable[hash];

    // insert temp node 
   while (temp_node->next != NULL)
        {
             temp_node = temp_node->next;
        }
           temp_node->next = new_node;   
        }
  counter++;
  }
return true;
}
25
  • If you are using gbd, it should tell you what line caused the seg fault.
    – David
    Nov 6 '15 at 2:13
  • found it, it's this line i think: temp_node -> next = new_node;
    – Steve23455
    Nov 6 '15 at 2:16
  • Shouldn't that be outside the while loop? Because you want to get to the end of the chain of nodes before you add on the new node?
    – David
    Nov 6 '15 at 2:23
  • I was just giving that a try, i had the while loop saying while(temp_node -> next != NULL) which it didn't like, so changed it back to while(temp_node != NULL) now it doesn't like the temp_node -> next = new_node; outside the while loop.
    – Steve23455
    Nov 6 '15 at 2:29
  • I'll come back later, but what is the error message?
    – David
    Nov 6 '15 at 2:32
0

Instead of

while (temp_node != NULL)
{
    temp_node = temp_node -> next;
    temp_node -> next = new_node;
}

Do

while (temp_node -> next != NULL)
{
    temp_node = temp_node -> next;
}
temp_node -> next = new_node;

Because what your code is saying is "once currentnode is null, add a node where it is pointing." However, it is NULL, so you don't want to put a node where currentnode is pointing, you want currentnode to NOT be NULL and you want what currentnode is POINTING to to be NULL. And you don't add a new node for every run through the while loop. You do it when the while loop has finished running, when currentnode is pointing to where newnode will be added.

node* temp_node = malloc(sizeof(node));

Also, skip this step. Tempnode is a pointer to a node, which you are using to navigate your linked list, so you don't have to create a new node for tempnode.

3
  • removing node* temp_node gives the following error when make is run dictionary.c:135:6: error: use of undeclared identifier 'temp_node'; did you mean 'new_node'? temp_node = hashtable[hash]; ^~~~~~~~~ new_node
    – Steve23455
    Nov 7 '15 at 16:17
  • I mean initialize tempnode with node* temp_node = hashtable[hash];. Don't delete the declaration, just don't allocate memory for tempnode.
    – David
    Nov 7 '15 at 16:26
  • still get seg fault but added gdb step through code to question, seems to be on third run through it happens, using small dictionary to load which has two words in it.
    – Steve23455
    Nov 7 '15 at 17:20

You must log in to answer this question.

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