-1

I'm having some trouble in the pset5, I actually don't know how to start debugging, I've watched the lessons a few times now and I'm not getting anywhere..

When I run speller.c it is giving me a seg fault, I ran the debugger and it crashes at the beginning of the For Loop, here follows my code:

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

#include "dictionary.h"
// default dictionary
#define DICTIONARY "dictionaries/large"

//created the struct node
typedef struct node
{
    bool is_word;
    struct node * paths[27];
}
node;

int letter = 0;
char * word = NULL;

/**
 * Returns true if word is in dictionary else false.
 */ 
bool check(const char *word)
{
//todo
return false;
}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{
//opens dictionary for reading
FILE *fp = fopen(DICTIONARY, "r");
if (fp == NULL)
{
    return false;
    unload();
}

//creates the root of the trie
node *root = malloc(sizeof(node));
root -> is_word = false;

node * trav = root;

char * word = NULL;

//start reading the file
while (fscanf(fp, "%s", word) != EOF)
{
    for (int i = 0; i < strlen(word); i++)
    {
        //assessing which path to take
        char c = fgetc(fp);
        if (isupper(c))
        {
            letter = tolower (c);
            letter = letter -'a';
        }
        else if (isalpha(c))
        {
            letter = c;
            letter = letter -'a';
        }
        else if (c == '\'')
        {
            letter = 26;
        }
        else if (c == '\0')
        {
            trav -> is_word = true;
        }
        if (trav -> paths[letter] == NULL)
        {
            node *new_node = malloc(sizeof(node));
            if (new_node == NULL)
            {
                return false;
               unload();
            }
            //point to new node
            trav -> paths[letter] = new_node;
        }
        else
        {
            trav = trav -> paths[letter];
        }
    }

}
if (fscanf(fp, "%s", word) == EOF)
{
    fclose(fp);
    return true;
}
return false;
}

/**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
 */
unsigned int size(void)
{
// TODO
return 0;
}

/**
* Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{
// TODO
return false;
}

I also don't know how to point the new_node to the next new node and if I must have different names for them. For example, I'm going to store the word "foo", so I read the node called trav, go to the path[5] (the f letter), check if it is already opened, if not (if it's NULL) I create a node called new_node and point trav -> paths[5] to it, than I should update trav to be the new node, so I point it to it's own path[letter]?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Unrelated to your problem, but you do know that `return` returns *immediately*? Any code in the same scope after the `return` will not be executed, it's so called *dead code*. In the `load` function you have such *dead code* if you fail to open the file. – Some programmer dude Oct 13 '17 at 07:47
  • As for your problem, where does `word` point when you pass it to `fscanf`? The `fscanf` doesn't allocate memory for you. – Some programmer dude Oct 13 '17 at 07:48
  • Sorry, I've updated the code pointing word and just forgot to post here: char * word = NULL; – Everton Dalagnol Oct 13 '17 at 07:52
  • Yes, `word` is a null pointer. And `fscanf` doesn't (can't really) allocate memory for that pointer to point to. So what happens when `fscanf` wants to dereference `word` to write the characters it reads? You can't dereference a null pointer, it leads to [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). I suggest you define `word` as an *array* instead. – Some programmer dude Oct 13 '17 at 07:56
  • Thanks! it worked!! =) – Everton Dalagnol Oct 13 '17 at 08:01

1 Answers1

-1

word is a NULL pointer. And fscanf doesn't (can't really) allocate memory for that pointer to point to. So what happens when fscanf wants to dereference word to write the characters it reads? You can't dereference a NULL pointer, it leads to undefined behavior. I suggest you define word as an array instead.

Note : Answer taken from comments

akshayk07
  • 2,092
  • 1
  • 20
  • 32