简体   繁体   中英

Segmentation Fault 11 with ANSI C Linked List

Updated with some new details:

Interestingly if I get no segfault if I don't use: node->next = NULL. I can create the nodes fine, but it seems there's an issue in setting the initial list head to my new node, as well as dereferencing ->next.

Note that without the structure definitions, it make be a bit harder for us to know what is wrong...

You have a problem here where you use the allocated pointer before testing whether the malloc() failed. Although that's probably not the culprit at this point.

struct node *head = (struct node*)malloc(sizeof(struct ftt_node));
foodList->head = head;      // <<<---- here using the pointer NULL or not
head->next = NULL;
rest->foods = foodList;

if (NULL == rest->foods) {       // <<<---- testing here if malloc() failed
    printf("List creation failed");
    return FALSE;
}

As a side note, the head pointer has the same problem.

Then in add_node you do that, with newNode never allocated... so probably garbage!

struct node *newNode;
newNode->data = newFood;

You probably wanted to allocate newNode and use curr to find the last existing node.

Something like this:

void add_node(POS * POS, struct food * newFood)
{
    struct node *newNode;

    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = newFood;

    curr = POS->foods->head;

    while (curr != NULL)
    {
        curr = curr->next;
    }

    curr->next = newNode;
    newNode->next = NULL;
}

That being said, I would very strongly suggest that you create a base list object with functions to handle the list, and then start your node with that list, instead of writing it this way.

As a side note: You should NOT name the variable POS in your add_node(). That's bad practice as it shadows your variable type.

Just wondering, why not use C++? At least testing for NULL is not required because new throws if memory cannot be allocated... And for list you have std::list, although in your case std::vector would probably work better so you could go through your array of nodes with a very simple for().

The add_node() function never modifies pos->foods->head . It also never initializes new_node , so that pointer cannot be dereferenced without triggering undefined behavior.

Thus, the list can never grow from being empty.

Your newNode variable in add_node is unitialised so it can point anywhere in memory, which can be the cause of the segfault when you try to dereference it on the second line. In addition, you allocated a new struct node structure but overwrote its adress immediately. So your first few lines should be rewritten thus:

struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = newFood;
struct node *curr = POS->foods->head;

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