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;
4
  • 2
    You didn't allocate for head (in main) nor *head (in create_index) Commented Jan 4, 2014 at 20:51
  • This: (*head)->empty = 1;' There's no valid memory pointer in *head` or head. Commented Jan 4, 2014 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... Commented Jan 4, 2014 at 21:01
  • You could use some readings about C. Some good reference is here: stackoverflow.com/questions/562303/… Commented Jan 4, 2014 at 21:10

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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
}

2 Comments

Note that the *head check in create_index() is not guaranteed to work since the pointer in main() is not initialized so it might not be a null pointer.
@JonathanLeffler Thank you for mentioning that. I update my answer.
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 ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.