Some misconception with linked list implementation

时间:2016-04-21 22:26:37

标签: c linked-list

It seems that I have a major misconception as to how I can implement a linked list in C. As far as I can understand (and this code is as simplified as can be!), this should work, but instead returns a segmentation fault. Also worth noting that if I comment out everything below MARKER, it seems to be working, but there is probably an underlying issue that makes it all faulty.

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


typedef struct node
{
    int val;
    struct node *next;
} node;


int main (void)
{
    node *pode;
    node *nx, *vx, *sx;

    pode = (node *) malloc(1 * sizeof(node));
    pode->val = 12;

    pode = (node *) realloc(pode, 2 * sizeof(node));
    pode->next = (pode + 1);
    (pode + 1)->val = 66;

    pode = (node *) realloc(pode, 3 * sizeof(node));
    pode->next = (pode + 2);
    (pode + 2)->val = 33;

    pode = (node *) realloc(pode, 4 * sizeof(node));
    pode->next = (pode + 3);
    (pode + 3)->val = 44;


    printf("%d\n", pode->val);

    nx = pode->next;
    printf("%d\n", nx->val);

    // MARKER
    vx = nx->next;
    printf("%d\n", vx->val);

    sx = nx->next;
    printf("%d\n", sx->val);

    return 0;
}

Where's the boo-boo?

2 个答案:

答案 0 :(得分:3)

Well, your first mistake is using realloc(). That's easy to get burned with because it might or might not move the node, causing any pointers to it to become dangerous dangling pointers.

Linked lists are all about linking the nodes together with pointers, so there's no reason to reallocate one node into a larger node, and that defeats the ability to splice the list by changing pointers. Generally, you'd allocate each node separately. (In some cases a program might deallocate list nodes by putting them into a "free" list for very fast deallocation and later reallocation.)

Your second mistake is forgetting to set each node's next pointer as soon as you allocate the node. Don't rely on the allocator to clear it.

Even if the code did set all the next pointers correctly, realloc() may move the node, breaking all those pointers.

Finally, if you step through the running program in a debugger (e.g. CLion), you'll be able to see where you have dangling pointers.

答案 1 :(得分:1)

这里有2个问题。为什么我的代码会崩溃?这是做链接列表的“正确”,“惯用”方式吗?

第二个第一个。不它不是。通常的方法是分别对每个节点进行malloc并将它们链接在一起。你真正构建的是一个可调整大小的数组。你需要

log.info
log.error
log.warning

找出代码崩溃的原因在调试器下运行它