I'm working through the "load" part of speller.c and running into problems... Here is a tiny snippet of my code to avoid breaking any rules: My hash_values are simple so A = 0, B = 1 C = 2 etc, and the hashtable size is [26]:

bool load(const char* dictionary)
FILE* file = fopen(dictionary, "r");
int a;
char* word_check1;
char word[LENGTH + 1];

for (int i = 0; i < 26; i++)
    hashtable[i] = NULL;

   *** a = fgetc(file);
    int hash_value = hash(word);

    // Capitalise all letters 
    for (int i = 0, n = strlen(word); i < n; i++)
        word_check1 = malloc(sizeof(word) + 1);
        word_check1[i] = toupper(word[i]);   
    node* new_node = malloc(sizeof(node));

    strcpy(new_node->word, word_check1);

    if (hashtable[hash_value] == NULL)
        hashtable[hash_value] = new_node;
        new_node -> next = NULL;
        new_node -> next = hashtable[hash_value];
        **hashtable[hash_value] = new_node;     
    count ++;
while (a != EOF);
return true;

I ran GDB and got a segmentation fault at where the triple asterisks are ***, and I have absolutely no idea why. Even stranger is that sometimes when I run GDB the segmentation fault doesn't occur until later (at the double **). Any comments and suggestions would be appreciated.

  • Hard to tell what's going on with the limited code. You said that the seg fault occurs at the line with the asterisks. Is it possible that it occurred at the line before? GDB displays the next line to be executed, not the one just completed. Also, what is the actual value in hash_value on entry to the else block? Finally, did you malloc some memory for new_node, is it set to null, or was it initialized at all after being declared? – Cliff B Aug 17 '15 at 18:28
  • I've deleted my previous comment because after running GDB again the segmentation fault starts at "a = fgetc(file)"... I have already declared the int "a" previously so I have no idea what's wrong... Your help is great appreciated, and please bear in mind I'm almost a complete beginner at coding, thank you. – Ivan Chan Aug 18 '15 at 2:10

Your error originates a lot earlier in the code. Look at the first two lines in your do loop:

a = fgetc(file);
int hash_value = hash(word);

Your first problem is that you only read a single character in from the file. You never actually read in a whole word anywhere.

Next, you are calling hash() based on having a word in the string var word, but you've never initialized word with any data, so it contains whatever garbage data that was in that memory location. When you run hash on it, you get a result based on that garbage data. It just goes downhill from here.

Also, you have one really big memory leak. You're malloc'ing word_check1 over and over in a loop. Each time you do, you're losing track of the allocation of memory from the previous pass through the loop. If that weren't enough, if a word had been loaded into word_check1, it would have been lost after the first pass through the loop.

There's a lot going on here that needs to be addressed, but this will at least get you started on rewriting your code.

If this answers your question, please click the check mark to accept this and remove the question from the unanswered pool. Let's keep up on forum maintenance. ;-)

  • Ah I see, thank you for your help. Following your advice I now use fscanf() instead of fgetc(), and the problem has improved dramatically - albeit segmentation fault still occurs if I load a large dictionary. I consider this problem solved, and if I can't figure out how to fix the new issue I'll ask a new question. I appreciate your help :) – Ivan Chan Aug 19 '15 at 4:53

You must log in to answer this question.

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