The following is my load function and I am having a segmentation fault when I run the executable code by typing for example when I type

./speller texts/aca.txt

When I run debug50, it is pointing to the for loop below for the reason of this segmentation fault. Essentially what I am trying to do here in this for loop is basically before any of the functions start to do their work, I ensure that each ith element of the array of node *'s (i.e each of table[i]'s) are pointing to NULL. The reason for doing this is because later on in my code I am using this to check if the table[i] already has a node * in it or not.

To me this logic makes sense since when the following code is written node *table[N] with N = 100; it simply creates 100 elements in the stack which are basically storing the memory address of 100 nodes. At this point, this memory address is simply some garbage value that the computer has inputted in it. So by using the for loop below, I am making sure that now all 100 of these elements are pointing to NULL.

Considering that I have a segmentation fault, I am clearly doing something wrong here, but can not seem to point to what's wrong with the logic. Can anyone provide some guidance as to what's wrong with my logic? Should I be (for each ith node *) first be mallocing with size of node in the for loop before equating it to NULL? That doesn't make a lot of sense but just thinking out loud here? Maybe the answer is in the following question - what happens in the RAM, when the following is written

node *table[100];

Thank you.

// The following is my load function
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)

    //The following for loop initializes each of the N (N = #_of_arrays_in_hash_table) elements of the table to be pointing to NULL;
    for (int i = 0; i < Num_of_arrays_in_hash_table; i++)
        table[i] = NULL;

    //Open file for reading
    dict_read = fopen(dictionary, "r");

    if(dict_read == NULL)
        printf("no memory for dict_read\n");
        return false;

    word_dict = malloc((1 + LENGTH) * sizeof(char));
    int x = feof(dict_read);

    while(x != 0)
        fscanf(dict_read, "%s" , word_dict);

        node *new_node = malloc(sizeof(node));

        if(new_node == NULL)
            printf("new_node failed\n");
            return false;

        // Declare the values of the node
        strcpy (new_node->word, word_dict);
        new_node->next = NULL;

        //Equating the hash_code into this new int declared hash_code
        int hash_code_dict = hash(word_dict);

        //The following if statement checks if the ith element (interchangeable with the hash_code) of the array is pointing to NULL or not.
        //if it is point to a NULL, this means that this is the first time this hash_code has been outputted from the hash function.

        if(table[hash_code_dict] == NULL)
            table[hash_code_dict] = new_node;

            new_node->next = table[hash_code_dict];
            table[hash_code_dict] = new_node;
    return true;
  • What is N (or Num_of_arrays_in_hash_table)? If it's greater than 100, seg fault. Make sure table is declared big enough to accommodate all the elements. It would be a good idea to use the same named variable in the declaration and the for loop. If it's N, use N for both. – DinoCoderSaurus Apr 21 '20 at 15:37
  • Take it a step further. First, kudos for using a very descriptive var name, but it is a bit long. Maybe a 1 or 2 word var name like "BUCKETS" would work better? So I'll use that. If you define a fixed value for the program, like DEFINE BUCKETS 100, then use that everywhere, including node *table[BUCKETS]; That way, you can change the array across the program by changing the BUCKETS definition to another number. It comes in handy when trying to do performance tuning. ;-) – Cliff B Apr 21 '20 at 16:36

You must log in to answer this question.

Browse other questions tagged .