1

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;
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Manos Ntoulias
  • 513
  • 1
  • 4
  • 21
  • 2
    You didn't allocate for `head` (in `main`) nor `*head` (in `create_index`) – Kninnug Jan 04 '14 at 20:51
  • This: `(*head)->empty = 1;' There's no valid memory pointer in `*head` or `head`. – lurker Jan 04 '14 at 20:52
  • The error is as the others have said. It might have been more obvious if you had initialized the value in your main call: HTinfo *head = NULL. Without an initial value, in "c", it may be anything... – FuzzyBunnySlippers Jan 04 '14 at 21:01
  • You could use some readings about C. Some good reference is here: http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – ericbn Jan 04 '14 at 21:10

3 Answers3

1

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
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

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
Kninnug
  • 7,992
  • 1
  • 30
  • 42
0
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 ...
ericbn
  • 10,163
  • 3
  • 47
  • 55