0

I have been trying with this code for some time and tried numerous permutations, however I am still struggling to get anywhere! I am very new to coding so please give any advice that will improve my skills. The code is as follows (segmentation fault indicated in code comments):

#include <stdbool.h>
#include <cs50.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h> 
#include <ctype.h>

#include "dictionary.h"

//definition of struct type
   typedef struct node
    {
        bool is_word;
        struct node* children[27];
    }
    node;

int index;
node* root;

/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char* word)
{
  node* trav = root;

   for(int i=0; i<strlen(word); i++)
   {
        //assign each element an index, including apostrophes
        if(word[i]== '\'')
        {
         index = 26;
        }

        else
        {
        index = tolower(word[i])-'a';
        } 
              //check if not in dictionary
        if(trav->children[index] == NULL) //SEGMENTATION FAULT!
        {
          return false;
        }

        trav=trav -> children[index];
    }

        if(trav->is_word == true)
        {
           return true;
        }

    return false;
   }

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{

    //gdefined root node
    node* root=NULL;
    //assign memory
    root = malloc(sizeof(node));

    //Open file

  // open input file 
    FILE* dict = fopen(dictionary,"r");

    //Check raw data is available
    if (dict == NULL)
    {
         fclose(dict);
        fprintf(stderr, "Could not open %s.\n","dictionary" );
        return 1;
    }

    //for each word read in, iterate through trie

    char temp[LENGTH+1];

    int wordcount = 0;

    while(fscanf(dict,"%s\n",temp) != EOF)
    {

    //Count words as we go

       wordcount++;

    //define traversal node

       node* trav = root;

       for(int i=0; i < strlen(temp); i++)

        {
            //assign each element an index, including apostrophes
        if(temp[i]== '\'')
        {
         index = 26;
        }
       else
        {
        index = tolower(temp[i])-'a';
        } 

        //if NULL malloc new node, have children i point to it
       if(trav->children[index]==NULL)
       {
          trav->children[index] = malloc(sizeof(node));
        }

        else
        {
         //if not NULL move to node and continue

            trav = trav->children[index];
        }

        //at end of work, set is_word to true

        if(i == strlen(temp)-1)
        {
            trav->is_word = true;
        }
        }

    }
    printf("%i", wordcount);
    return true;
}

/**
2

It's a common problem. The problem lies in load(), where you have created a "shadow variable."

A shadow variable happens this way: First, the original variable is created, either earlier in main or a function, or as a global variable. Then a second variable is created with the same name, but will have local scope because it's within a set of curly braces and the original var is outside those braces. The original variable, with more global scope is masked by the local shadow variable that takes precedence.

In this case, you've created a global variable called root. Then, you create a shadow var of root inside load().

node* root=NULL; 

All of the work is done with the shadow root to build the trie, but when load ends, the shadow var ceases to exist and the entire trie is lost. All this time, the global root remains and is still set to NULL (or garbage data). When check() is called, the code hits the line if(trav->children[index] == NULL). Since trav==root==NULL, the struct, including the children[ ] pointers, doesn't exist, so it generates a seg fault.

If you remove node* from the line in load(), the problem goes away.

There are more issues, but those would be new questions. If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;)

1
  • That's great Cliff I would have never resolved that on my own. Greatly appreciated!! – Andrew Doherty Nov 29 '16 at 12:26

You must log in to answer this question.

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