简体   繁体   中英

Accessing a struct causes segmentation fault

Why am I getting a segmentation fault when I go to access the struct vm ? The code is as follows:

BOOLEAN vm_init(struct vm * vm)
{
    struct vm_node * vmNode;
    vmNode = malloc(sizeof(struct vm_node));
    vm->item_list->head = vmNode;
    vm->coinsfile = " ";
    vm->foodfile = " ";
    return FALSE;
}

/* 
 * Loads data from the .dat files into memory. 
 * */
BOOLEAN load_data(struct vm * vm, const char * item_fname, 
                  const char * coins_fname) {
    FILE *file;
    file = fopen(item_fname, "r+");
    char buf[256]={};
    struct vm_node *vmNode;
    vmNode = malloc(sizeof(struct vm_node));
    vmNode->next = NULL;

    while (fgets(buf, sizeof buf, file) != NULL) {
        addNodeBottom(buf,vmNode);
    }

    /* Test reason for reaching NULL. */
    if (feof(file)) /* if failure caused by end-of-file condition */
    {
    }
    else if (ferror(file)) /* if failure caused by some other error      */
    {
        perror("fgets()");
        fprintf(stderr, "fgets() failed in file %s at line # %d\n", __FILE__,
                __LINE__ - 9);
        exit(EXIT_FAILURE);
    }
    fclose(file);

If I try to access vm->item_list->head it segfaults. item_list is the container to a linked list, which is what vmNode is. So i need to store vmNode within vm->item_list->head .

But if I do the following:

vm->item_list->head = vmNode; //it segfaults... 

Any clues?

The typedefs for vm and vm_node are as follows.

struct stock_item
{
    char id[IDLEN+1];
    char name[NAMELEN+1];
    char description[DESCLEN+1];
    struct price price;
    unsigned on_hand;
};

/* The data structure that holds a pointer to the stock_item data and a
 * pointer to the next node in the list
 */
struct vm_node
{
    struct stock_item * data;
    struct vm_node * next;
};

/* The head of the list - has a pointer to the rest of the list and a 
 * stores the length of the list 
 */
struct vm_list
{
    struct vm_node * head;
    unsigned length;
};

/* This is the head of our overall data structure. We have a pointer to 
 * the vending machine list as well as an array of coins. 
 */
struct vm
{
    struct vm_list * item_list;
    struct coin coins[NUMDENOMS];
    char * foodfile;
    char * coinsfile;
};
/* This is the head of our overall data structure. We have a pointer to 
 * the vending machine list as well as an array of coins. 
 */
struct vm
{
    struct vm_list item_list;
    struct coin coins[NUMDENOMS];
    char * foodfile;
    char * coinsfile;
};

One little fix. The item_list is just a pointer and an int. This allocates the memory for it within the struct vm
You had the pointer but not the structure it should be pointing to.

You will now have to use

vm.item_list->head = vmNode;

when setting the initial node.

And that should solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM