0

I've been getting a seg fault in load on this line: buckets[hash_num] = buffer_node;.

I have read most if not all the posts on seg faults in load, and can't seem to figure mine out. At first I thought it was because the pointer buckets[hash_num] was not allocated memory, so by assigning buffer_node to it, I was creating the seg fault. However when I tried to malloc memory to buckets[hash_num] (as seen in the commented out code below), the code does not even compile.

A check on valgrind shows there are no errors.

I don't know if this matters, but the segfault happens only after I've successfully loaded a few words into dictionary and I hit the first capitalized word.

Any help would be much appreciated. I'm totally new to programming. I've been at this for days now and I am quite clueless how to proceed.

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

//prototypes and global variables
int hash_num;
int isupper(int c);
int hash(const char* dictionary_word);
int words_loaded = 0;

// Create array of buckets, one for each hash
node* buckets[26];

// I tried to malloc each element in array, but code wouldn't compile. 
//for (int i = 0; i < 26; i++)
//{
//    buckets[i] = malloc(sizeof(node));
//}

bool load(const char* dictionary)
{
    //struct defined on top
    //array of buckets / pointers to linked list declared globally
    //Open Dictionary
    FILE* dictionary_ptr = fopen(dictionary, "r");
    if (dictionary_ptr == NULL)
    {
        printf("fail to open dictionary\n");
        return false;
    }

    //array to store word
     char buffer[LENGTH + 1];

    //malloc a node called buffer_node for the new word
    node* buffer_node = malloc(sizeof(node));

     //scan dictionary file, load words into buffer
    while (fscanf(dictionary_ptr, "%s", buffer) == 1)
    {
        //generate hash number of buffer
        hash_num = hash(buffer);

        // copy the buffer word into the node field
        strcpy(buffer_node -> dictionary_word, buffer);

        //link node's pointer to NULL if that bucket is empty, else link it     to the first node in bucket. 
        if (buckets[hash_num] == NULL)
        {
            buffer_node -> next = NULL; 
        }
        else
            buffer_node -> next = buckets[hash_num];
        buckets[hash_num] = buffer_node; //keep getting a seg fault here

        //clock word upload
        words_loaded += 1;
        printf("%i, %s saved to memory success\n", words_loaded, buffer);

        //jump to next line
    }
    fclose(dictionary_ptr);
    printf("dictionary saved successfully\n");
    free(buckets);
    free(buffer_node);
    return(true);
}

1 Answer 1

1

You were on the right track. buckets is declared as an array of pointers, but they're not initialized, so they contain garbage data. They do need to be initialized. Next, the compiler is choking because of where you are trying to initialize buckets. Only certain actions, like declaring a global variable are allowed to be done outside of main or a function. A for loop is not allowed outside of main or a function.

Move the initialization to the beginning of load(). Also, have you considered initializing buckets by setting each node to null instead of malloc?

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

2
  • Hi Cliff, I did as you suggested(setting the array of points to NULL, with a for loop inside load function). Dictionary.c compiles fine now, but for some reason i'm still facing a seg fault at the same line. This is the error message in gdb: Program received signal SIGSEGV, Segmentation fault. 0x00000000004013f7 in load (dictionary=0x7fffffffe2e0 "dictionaries/small") at dictionary.c:106 106 buckets[hash_num] = buffer_node; //keep getting a seg fault here (gdb)
    – April
    Commented Jul 1, 2016 at 16:02
  • Can't help without seeing current code. But it sounds like buckets[hash_num] = = NULL or your hash function is returning a number outside of 0 to 25.
    – Cliff B
    Commented Jul 1, 2016 at 17:13

You must log in to answer this question.

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