0

This is my code for load() function. When I am doing "./speller text" it is showing Segmentation fault. Please help.

typedef struct node
{
    char name[LENGTH+1];
    struct node* next;
}node;

node* root[26];


bool load(const char* dictionary)
{
    int ii=0;
    while(ii<26)
    {
    root[ii] = malloc(sizeof(node));
    root[ii] = NULL;
    ii++;
    }
    FILE* file = fopen(dictionary,"r");
    if(!file)
        return false;
    char c;
    char nm[LENGTH+1];
    int i=0,index;
    node* temp=malloc(sizeof(node));
    temp=NULL;
    while((c = fgetc(file)) != EOF)
    {   
            if(i==0)
                strcpy(nm,"");
            if(c!='\n')
                {
                    nm[i]=c;
                    i++;

                }  

            else
            {
                nm[i]='\0';
                i=0;
                index=nm[0]-'a';

                if(!root[index])
                {   
                    temp=malloc(sizeof(node));
                    strcpy(temp->name,nm);
                    root[index]->next=temp;
                    temp->next=NULL;
                    free(temp);
                }
                else
                {   
                    temp=malloc(sizeof(node));
                    node*temp1=root[index];


                while(temp1->next!=NULL)
                    {
                        temp1=temp1->next;

                    }
                strcpy(temp->name,nm);
                temp1->next=temp;
                temp->next=NULL;
                free(temp1);
                free(temp);
            }
  }
}
  return true;                
  fclose(file);
}

And also when I,m doing "make dictionary" it is giving this error.

clang -ggdb3 -O0 -Qunused-arguments -std=c11 -Wall -Werror   -c -o dictionary.o dictionary.c
clang   dictionary.o  -lcs50 -lm -o dictionary
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [dictionary] Error 1
3

The seg fault happens because the code is trying to make an assignment to something that doesn't exist. Look at the following code block:

            if(!root[index])
            {   
                temp=malloc(sizeof(node));
                strcpy(temp->name,nm);
                root[index]->next=temp;

The if condition checks to see if root[index] is null. If it is, then it executed the code that follows. When it gets to root[index]->next=temp; it is trying to assign temp to next, but since root[index] is null, root[index]->next doesn't exist. That causes a seg fault.

The compile errors from make dictionary occur because there is no main() in dictionary. You should be doing make speller instead. One of the lessons in this pset is how to do projects with multiple source files. A review of the contents of the file Makefile and the class material on 'make' would be helpful.

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

You must log in to answer this question.

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