0

Getting a segfault after about 75 words added to the dictionary.

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

typedef struct node {  
    bool isWord;  
    struct node* keys[27]; } node;

typedef struct list {  
    node* pointer;  
    struct list* previous; } list;  

node* first  =NULL;    
list* thisList = NULL;
int sizer;
/**  * Returns if word is in dictionary else false.  */
bool check(const char node* current = first;
    int translated;
    for (int c=0;word[c]!='\0';c++) {
        if (word[c]<91) translated=word[c]-65;
        else if (word[c]==96) translated=26;
        else translated=word[c]-97;
        if(current->keys[translated]!=NULL) {
            current=current->keys[translated];
        }
    }
    return current->isWord; }

/**  * Loads dictionary into memory. Returns true if successful else false.  */     
bool load(const char *dictionary) {
    FILE *fp = fopen(dictionary, "r");
    if (fp == NULL) {
        printf("Could not open %s.\n", dictionary);
        fclose(fp);
        return false;
    }
    //initialize the first (pointer) node(which cannot be a word itself)
    first=malloc(sizeof(node));
    if (first==NULL) return false;
    for (int i = 0; i<27;i++) {
        first->keys[i]=NULL;
    }
    first->isWord=false;
    node* thisNode=first;
    thisList=malloc(sizeof(list));
    if (thisList==NULL) return false;
    thisList->pointer=NULL;
    thisList->previous=NULL;
    list* temp=NULL;
    for (int c = fgetc(fp); c != EOF; c = fgetc(fp)) {
        if (c != '\n') {
            //map chars to {0...26}
            if (c<91) c-=65;
            else if (c==96) c=26;
            else c-=97;
            //allocates memory for a new node in an 
            //appropriate place (depending on what char it is)
            if (!thisNode->keys[c]) thisNode->keys[c]=malloc(sizeof(node)); 
            //collect all the pointers into 
            //linked list for quicker unloads
            //later on
            thisList->pointer=thisNode;
            temp=thisList;
            thisList=malloc(sizeof(list));
            if (thisList==NULL) return false;
            thisList->previous=temp;
            //next node is set as current
            thisNode=thisNode->keys[c];
            //give all pointers a NULL for 
            //safety purposes
        } else {// we must have found a whole word
            //so bool is true
            thisNode->isWord=true;
            //again adding pointer for later release
            thisList->pointer=thisNode;
            temp=thisList;
            thisList=malloc(sizeof(list));
            if (thisList==NULL) return false;
            thisList->previous=temp;
            //reseting the whole thing in order to 
            //correctly process the next word
            thisNode=first;
            sizer++;
        }
    }
    return true; }
1

Apostrophe problem. Apostrophe is 39 in ascii. (96 is backwards apostrophe, or back-tick).

When check encounters apostrophe here if (word[c]<91) translated=word[c]-65; translated is a negative number.

There is the same problem in load.

1
  • thank you so much i've been trying to beat this task for a few days now – Semyon Kamoza May 28 '17 at 21:29

You must log in to answer this question.

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