0

I am not getting where the segmentation fault is happening.

// Implements a dictionary's functionality
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include "dictionary.h"

char mug[LENGTH + 1] = {'\0'};  //Temporary mug for one dict words
unsigned int track_of_dict_words = 0;
int c, k = 0, j = 0, index = 0, p = 0;

//Creating nodes
struct node *getNode(void)
{
    struct node *pNode = NULL;
    pNode = (struct node *)calloc(1, sizeof(struct node));
    if (pNode)
    {
        pNode -> is_word = false;
        for(int i = 0; i < 27; i++)
            pNode -> children[i] = NULL;
    }
    return pNode;
}

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    struct node *pCrawl = root;
    while(word[p] != '\0')
    {
        if (isalpha(word[p]) || (word[p] == '\''))
        {
            if(isalpha(word[p]))
                index = ((int)(tolower(word[p])) - (int)'a');
            else
            {
                index = 26;
            }
            //printf("%i ", index);


            if ((pCrawl -> children[index]) != NULL)
            {
                pCrawl = pCrawl -> children[index];
            }
            else
            {
                return false;
            }
        }
        ++p;
    }
    //printf("\n-----  ");

    return pCrawl -> is_word;
}


//Inserting values in dictionary in the memory
void insert(const char *key)
{

    //Determining the length of the current word
    int length = 0;
    index = 0;
    for(int i = 0; i < LENGTH; i++)
    {
        if (key[i] == '\0')
            break;
        ++length;
    }

    struct node *pCrawl = root;

    for(int l = 0; l < j; l++)
    {
        if (isalpha(key[l]) || (key[l] == '\''))
        {
            if(isalpha(key[l]))
                index = ((int)key[l] - (int)'a');
            else
                index = 26;
                //printf("%d\n", index);

            printf("\n");
            /*for(int asp = 0; asp < 26 ; asp++)
            {
                if (index == asp)
                    printf("%i", asp);
                else
                    printf("^");
            }*/


            if ((pCrawl -> children[index]) == NULL)
                pCrawl -> children[index] = getNode();

            pCrawl = pCrawl->children[index];
        }
    }

    //Marking last index as true to make it a word in dictionary
    pCrawl -> is_word = true;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    root = getNode();

    //Opening dictionary file
    FILE *inptr = fopen(dictionary, "r");
    if (inptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not open %s.\n", dictionary);
        return 3;
    }

    j = 0;
    for (c = fgetc(inptr); (c != EOF); (c = fgetc(inptr)))
    {
        if ((c == '\n') || (c == EOF))
        {
            ++track_of_dict_words;
            mug[j] = '\0';
            insert(mug);  //Inserting into Trie
            j = 0;
            /*for(int i = 0; i < LENGTH + 1; i++) //Reinitializing all elements with null
                mug[i] = '\0';*/
        }
        if (isalpha(c))
        {
            mug[j] = c;
            ++j;
        }
    }

    if (c == EOF)
    {
        printf("\nLoad Successful.");
        return true;
    }

    else
        return false;
}

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

bool flush(struct node *voot)
{
    if (root == NULL)
        return true;
    for (int i = 0; i < LENGTH; i++)
    {
        flush(voot -> children[i]);

    }
    free(voot);
    return true;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    flush(root);
    return true;
}
}

On using dictionaries/small on cat.txt I am getting this output.

MISSPELLED WORDS

A
is
not
a
caterpillar
dictionary.c:163:23: runtime error: member access within null pointer of type 'struct node'
Segmentation fault
  • Hi Pankaj, welcome to Stack Exchange. I tried to read your code, but it is hard to understand what the meaning of your global variables is. For instance, your function insert() suddenly uses some global variable j. Where did that variable get it's value? And couldn't you instead have used length (which you just computed, a couple of lines earlier)? – Peter Pesch Oct 10 '18 at 11:21
  • Also, I couldn't figure out where you converted your input text to lowercase. An uppercase character might cause problems when you want to use node->children ... – Peter Pesch Oct 10 '18 at 11:23
  • Hi Peter. Thank you for helping out. j was another implementation to get length. I have replaced j with length which is calculated before. Still same segmentation error. – pankaj Oct 11 '18 at 6:35
  • Also, tolower() has been used in check() function for calulating index by arithematic. – pankaj Oct 11 '18 at 6:38
  • I have figured the first culprit in unload() I am looping in flush() till LENGTH which is 45. It should be 26 times only. Still, this one is slow & throws seg fault after a long running time. – pankaj Oct 11 '18 at 7:59
1

The error is in this part:

bool flush(struct node *voot)
{
    if (root == NULL)
        return true;
    for (int i = 0; i < LENGTH; i++)
    {
        flush(voot -> children[i]);

    }
    free(voot);
    return true;
}

The error message points to line 163, which is this line:

        flush(voot -> children[i]);

This line makes sure that the remainder of the linked list is also freed. This is suppose to stop at the end of the linked list, but is doesn't ...

A the end of the linked list, you check if (root == NULL) in stead of if (voot == NULL).

You should always try to keep your variable names as clear as possible. That way, you might prevent accidentally using j in stead of length or root in stead of voot.


Finally, to quote Cliff B:

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

| improve this answer | |
  • It plugged a lot of holes & now there is no seg fault. It even runs fine and displays all parameter. But still, it displays more words as incorrect then staff's link – pankaj Oct 11 '18 at 8:39
  • Hi pankaj, your program displays more words as incorrect because of an error in your check function. I'm not allowed to answer the new question here, because this page is about an older question. If you create a new question for this, it will probably be answered soon. – Peter Pesch Oct 11 '18 at 9:25
  • Anyways Thanks Peter! I mean it. I have done amendments as per your suggestions and some more. Now I am getting to all right outputs. Only valgrind is not complementing the code. I will be posting a new question soon. – pankaj Oct 11 '18 at 12:15

You must log in to answer this question.

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