Question

I compiled this small amount of C code and when I run it, I get a segmentation fault.

It stops in the first lines of the create_index() function even though I have permission to access the addresses of the pointers. I have no clue what's going on.

main.c

 #include <stdio.h>
 #include "hash.h"

int main(void)
{       HTinfo *head;
        create_index(&head);
        return 0;
}

hash.c

 #include <stdio.h>
 #include <stdlib.h>
 #include "hash.h"

int create_index(HTinfo **head)
{       int i;
        (*head)->empty = 1;
        (*head)->index = malloc(101*sizeof(nodeptr));
        if ((*head)->index == NULL)
            return -1;
        for (i=0; i < 101; i++)
            (*head)->index[i] = NULL;
        return 0;
}

hash.h

typedef struct rc *nodeptr;

typedef struct rc {
    char ln[25];
    char fn[15];
    char city[25];
    char prefecture[3];
    int debt;
    int afm;
    nodeptr next;
} record;

typedef struct ht {
        nodeptr *index;
        char empty;
} HTinfo;
Was it helpful?

Solution

You didn't allocate memory for head. Either malloc in main:

HTinfo * head = malloc(sizeof *head);
if(NULL == head) // handle out-of-memory

or in create_index:

*head = malloc(sizeof **head);
if(NULL == *head) // handle out-of-memory

OTHER TIPS

First thing, when you run into a segmentation fault, try to debug your code using a memory debugger like valgrind. Most of the time, it will point out the erroneous code block.

From the code you have posted, it can be seen that you did not allocate any memory for HTinfo *head and tried to pass the address (which is invalid) to create_index() and then, without any check, you de-referenced it. As the memory location is not valid, this is an undefined behavior scenario, most likely lead to a segmentation fault, just as in your case.

Also, it is a best practice to always initialize a pointer when declared and check the validity of an incoming pointer variable to a function. You should initialize head in main() with HTinfo *head = NULL and might want to add a NULL check in the create_index() function itself, which will help you detecting passing invalid (NULL) address to the function (in case you forget to allocate memory) and will also prevent your code from crashing. You can use something like

if (*head != NULL)
{
     //your code
}
else
{
     //print appropriate error message, return/exit
}
HTinfo *head;

In main(), you are allocating a pointer to HTInfo structure. You could rather allocate the whole structure statically and pass a pointer to create_index()

int main(void)
{
    HTinfo head;
    create_index(&head);
    // continue ...
    // you should be testing for the return value of create_index()

And deal with the pointer in your create_index()

int create_index(HTinfo *phead)
{
    int i;
    phead->empty = 1;
    phead->index = calloc(101, sizeof(nodeptr));
    if (phead->index == NULL)
        return -1;
    // continue ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top