0

So I've been working my way through PSet5, and I can't seem to find out why I have a segmentation fault here, or why I get an endless loop. It says the fault is on line 31, which I'll mark in the code (it's part of check()), but I also copied load() just in case there's a part of the problem there too.

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

#include "dictionary.h"
typedef struct node {
  bool is_word;
  struct node* children[27];
}
node;

node* root;
int wordNum = 0;
/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char* word)
{
    // TODO
    node* character = root;
    for (int i = 0; i < strlen(word); i++) {   //this seems to be the line with the segmentation fault
        int chara = toupper(word[i]) - 'A';
        if(character->children[chara] != NULL) {
            character = character->children[chara];
        } else {
            return false;
        }
    }

    return character->is_word;
}

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{
    // TODO
    FILE* dict = fopen(dictionary, "r");
    node* character = root;
    wordNum = 0;
    for (int c = fgetc(dict); c != EOF; c = fgetc(dict))
    {
        if(c != '\n') {
            int chara = toupper(c) - 'A';
            if(character->children[chara] == NULL) {
            character->children[chara] = (struct node*) malloc (sizeof(node));
        }
            character = character->children[chara];
        } else {
            character->is_word = true;
            character = root;
            wordNum++;
        }

    }
    if(fgetc(dict) == EOF) {
    return true;
    } else {
    return false;
    }
}

If someone could give me some pointers (ba dum tsss) to help me get on the right way and solve my problem, that would be great. I've been at this specific problem for the last few hours now.

1 Answer 1

1

I don't understand why you are getting to check, or appear to be getting an infinite loop. When I ran your posted code, it immediately generated a seg fault. The problem is exposed here:

for (int c = fgetc(dict); c != EOF; c = fgetc(dict))
{
    if(c != '\n') {
        int chara = toupper(c) - 'A';
        if(character->children[chara] == NULL) {

The last line is generating the seg fault. When adding the first word, root is null. (This is interesting in itself, because it should contain garbage data, since it has never been initialized.) In any case, no memory has been assigned to root, so the root node doesn't yet exist, and it's children don't yet exist either. When the if statement tries to access a node element, ->children[], it seg faults.

There may be other issues, but this should get you going. If this answers your question, please click the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • problem->next = smaller; Thank you, your answer helped me with my comprehension of nodes, as well, so that's great. Jun 20, 2016 at 17:06

You must log in to answer this question.

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