0

So I have been getting a segmentation fault somewhere in my code. WHen I run the debugger, it gives me the following:

/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 17 has invalid symbol index 13 /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function _start': (.text+0x20): undefined reference tomain' clang: error: linker command failed with exit code 1 (use -v to see invocation) make: *** [dictionary] Error 1 Please be sure to stop other debuggers before continuing.

the following is my current code:

include

include

include

include

include

include "dictionary.h"

typedef struct node { char* word; struct node* next; } node;

node* table[27] = { NULL };

int sized = 0;

/** * Returns true if word is in dictionary else false. / bool check(const char word) { int wlen = strlen(word); //char* newword = malloc(sizeof(char) * wlen); char* newword = malloc(sizeof(char) * wlen); for(int i = 0; i < wlen; i++) { newword[i] = word[i]; } //newword[wlen] = '\0'; int num = hashByFirstLetter(newword); node* curr = table[num]; while(curr != NULL) { if(strcmp(newword, curr->word) == 0) { return true; } else { curr = curr->next; } } return false; }

/** * Loads dictionary into memory. Returns true if successful else false. / bool load(const char dictionary) { FILE* file = fopen(dictionary, "r"); if(file == NULL) { return false; }

char buffer[47];
while (fgets(buffer, 47, file))
{
    buffer[strlen(buffer)-1] = '\0';
    sized = sized + 1;

    node* newNode = malloc(sizeof(node));
    strncpy(newNode->word, buffer, 47);
    newNode->next = NULL;

    int binnum = hashByFirstLetter(buffer); 

    if(table[binnum] == NULL)
    {
        table[binnum] = newNode;
    }
    else
    {
        node* cursor = table[binnum];
        while(cursor->next != NULL)
        {
            cursor = cursor->next;
            //printf("%s\n", cursor->word);
        }
        cursor->next = newNode;        
    }
}
fclose(file);
return true;

}

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

/** * Unloads dictionary from memory. Returns true if successful else false. / bool unload(void) { // for each entry in the table... for (int i = 0; i < 27; i++) { // get the head of the list node wordnode = table[i];

    // walk over the entire list and free all the nodes
    while (wordnode != NULL)
    {
        // grab a reference to the next node (we'll need this in a sec)
        if(wordnode->next != NULL)
        {
            node* next = wordnode->next;
        // free the current node and its associated data
        free(wordnode->word);
        free(wordnode);

        // walk to the next node
        wordnode = next;
        }
        else
        {
            free(wordnode);
        }
    }
}
return true;

}

int hashByFirstLetter(char* inputString) { // get the first letter char firstChar = *inputString;

if (!isalpha(firstChar))
{
    return 27;
}

// we have a letter. return its alphabet position
int ascii = (int)(toupper(firstChar));
return ascii - 'A';

}

Can someone please help me find what is causing the segmentation fault?

2
  • 1
    One of the most important skills in programming is being able to identify which lines of code are causing problems. A seg fault is a golden opportunity for this. You need to start by identifying which function is failing, and then narrow it down. If you can't do it with gdb, you can always add strategically placed printf statements to print out messages like "start load", "end load", etc. So, which function is causing the seg fault and which line in that function?
    – Cliff B
    Jun 10, 2016 at 1:44
  • Thank you Cliff. I was already able to solve the problem, but I did start to use more printf statements in other psets and that has helped me immensely in finding bugs and errors. If anyone is curious, out of several changes I made to the code, the one I think actually fixed it was "newNode->word = malloc(strlen(buffer) + 1);" I didnt realize I should allocate memory to the char* word part of the node, I mistakenly assumed malloc'ing the node would take care of that. Jun 12, 2016 at 23:07

1 Answer 1

0

Asked and answered!

One of the most important skills in programming is being able to identify which lines of code are causing problems. A seg fault is a golden opportunity for this. You need to start by identifying which function is failing, and then narrow it down. If you can't do it with gdb, you can always add strategically placed printf statements to print out messages like "start load", "end load", etc. So, which function is causing the seg fault and which line in that function? – Cliff B 2 days ago

Thank you Cliff. I was already able to solve the problem, but I did start to use more printf statements in other psets and that has helped me immensely in finding bugs and errors. If anyone is curious, out of several changes I made to the code, the one I think actually fixed it was "newNode->word = malloc(strlen(buffer) + 1);" I didnt realize I should allocate memory to the char* word part of the node, I mistakenly assumed malloc'ing the node would take care of that. – Rafael Rossi Silva 1 min ago

If this answers your question, please click on the check mark to accept and remove from the unanswered question pool. 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 .