0

Hey guys so I'm working on pset5 and everytime I run my speller with a large dictionary and austinpowers.txt my program gets killed. I've ran valgrind with speller and there are no errors (but also no memory allocations and no frees strangely). I also tried to run check cs50 but it gives me a CERT_NOT_VALID error. I don't really know what is wrong with my code. Could a kind soul please take a look and point me in the right direction? Thanks! (I will delete my code later)

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


/**
 * Returns true if word is in dictionary else false.
 */
//define a trie

typedef struct node
    {
    bool is_word;
    struct node* children[27];

    }
    node;

int nwords = 0; //initialise number of words
node* root; //initialise root
bool check(const char* word)
{
    // TODO
    node* crawler = root; 
    for (int i = 0; i < strlen(word); i++) //iterate through a word
    {
      int letter = tolower(word[i]); //making character lower case
      if (letter == '\'')
      {
         return false;
      }
      if (crawler->children[letter-'a'] == NULL) //if path doesn not     exist, return false
      {
        return false;
      }
      else
      {
        crawler= crawler->children[letter];
      }

    }
    if (crawler->is_word)
    {
      return true;
    }
    else
    {
      return false;
    }
}

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{
    // TODO
    FILE* dict = fopen(dictionary,"r"); //open dictionary
    root = malloc(sizeof(node)); 
    if (dict == NULL)
    {
      return false;
    }
    //read till end of file
    int c = 0;
    while((c= fgetc(dict)) != EOF)
    {
      node* crawler = root;
      for  (; c != '\0'; c = fgetc(dict)) //iterate through word
      {
         if(crawler->children[c-'a'] == NULL) //if node does not exist, malloc     memory to said node.
        {
          crawler->children[c-'a'] = malloc(sizeof(node));
        }
        crawler = crawler->children[c-'a']; 
      }
      crawler->is_word = true; //set end of word to true
      nwords++;
    }
    fclose(dict);
    return true;
 }

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

/**
 * Unloads dictionary from memory.  Returns true if successful else false.
 */
void unload_node(node* crawler)
{
for (int i = 0; i < 27; i++) //iterate through all the children
{
  if (crawler->children[i] != NULL)
  {
    unload_node(crawler->children[i]); //use recursion
  }
}
free(crawler); //free node from bottom

}
bool unload(void)
{
    // TODO
    unload_node(root);
    return true;
}    

1 Answer 1

1

The problem is mainly caused by the fact that you are assuming that c is always a lower case char and therefore subtracting 'a' from it would give you a valid array index, but that is a wrong assumption. I consider detecting the problem in this situation is also a hint to the solution.

You must log in to answer this question.

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