io - C trie node reassignment causing segmentation fault -
i trying implement spellchecker, , 1 step load dictionary trie structure. have used gdb determine that, understanding, getting segmentation fault every time try assign current->children
value. full code @ bottom, method in question:
bool load(const char* dictionary) { file* dic = fopen(dictionary, "r"); if(dic == false) { return false; } root = calloc(27, sizeof(node)); node* current = null; /**for(int i=0;i<27;i++) { current->children[i]=null; }*/ //this location of segmentation fault if uncommented int = 0; while((a = fgetc(dic)) != eof) { if (a == '\n') { //this end of word if(!current->is_word) { //duplicate case current->is_word = true; wordcounter++; } current = root; } else { if(current->children[a-'a'] == null) { current->children[a-'a'] = calloc(27,sizeof(node)); } current = current->children[a-'a']; } } if(current!= root && !current->is_word) { current->is_word = true; wordcounter++; } fclose(dic); return true; }
there commented code there tried implement after checking couple other answers on stackoverflow, causes segmentation fault occur @ for
loop. otherwise, occurs @ if(current->children[a-'a']==null){...}
what's happening here? thought calloc()
automatically set assigned memory 0? memory touching i'm not supposed to?
full .c below:
#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include "dictionary.h" typedef struct node { bool is_word; struct node* children[27]; } node; node* root; int wordcounter=0; //returns true if word in dictionary else false. bool check(const char* word) { node* current = root; int b = 0; while(word[b] != '\n') { int letter = tolower(word[b]); if(letter == '\'') { return false; } if(current->children[letter-'a'] != null) { current = current->children[letter-'a']; b++; } else { return false; } } if(current->is_word == true) { return true; } return false; } // loads dictionary memory. returns true if successful else false. bool load(const char* dictionary) { file* dic = fopen(dictionary, "r"); if(dic == false) { return false; } root = calloc(27, sizeof(node)); node* current = null; /**for(int i=0;i<27;i++) { current->children[i]=null; }*/ int = 0; while((a = fgetc(dic)) != eof) { if (a == '\n') { //this end of word if(!current->is_word) { //duplicate case current->is_word = true; wordcounter++; } current = root; } else { if(current->children[a-'a'] == null) { current->children[a-'a'] = calloc(27,sizeof(node)); } current = current->children[a-'a']; } } if(current!= root && !current->is_word) { current->is_word = true; wordcounter++; } fclose(dic); return true; } //returns number of words in dictionary if loaded else 0 if not yet loaded. unsigned int size(void) { return wordcounter; } //unloads dictionary memory. returns true if successful else false. void memfree(node* current) { for(int = 0; < 27; i++) { if(current->children[i] !=null) { memfree(current->children[i]); } } free(current); } bool unload(void) { memfree(root); return true; }
the error happens because you're initializing current = null
, dereferencing (current->children
). it's straight null pointer dereference.
looking @ rest of code, meant do
node *current = root;
instead.
Comments
Post a Comment