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 to
main'
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?