双向链表 C - 变量 'list' 周围的堆栈已损坏

Doubly Linked List C - stack around variable 'list' was corrupted

我正在编写一个代码,将双向链表 list 分成两个列表 listAlistB 并将它们打印出来。代码似乎可以完成工作,但最终程序崩溃了。调试器抛出 Run-Time Check Failure #2 - Stack around the variable 'listA' was corrupted.Run-Time Check Failure #2 - Stack around the variable 'list' was corrupted. 我读到这可能是因为没有为我的结构分配足够的内存,但我应该分配多少?

完整代码:

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

typedef struct node {
    int val;
    struct node* prev;
    struct node* next;
}Node;
typedef struct list {
    Node* head;
    Node* tail;
}List;

void init(List* l) {
    l->head = NULL;
    l->tail = NULL;
}
Node* create(int val) {
    Node* ptr = (Node*)malloc(sizeof(Node));
    ptr->val = val;
    ptr->next = NULL;
    ptr->prev = NULL;

    return ptr;
}
void printList(const List* list) {

    Node *ptr = list->head;
    while (ptr != NULL) {
        printf("%i ", ptr->val);
        ptr = ptr->next;
    }
    puts("");
    free(ptr);
}
void pushLast(List* l, Node* node) {

    if (l->head == NULL) {
        l->head = node;
        l->tail = node;
    }
    else {
        node->prev = l->tail;
        l->tail->next = node;
        l->tail = node;
    }
}
void splitList(const List* list) {

    List* listA;
    List* listB;
    init(&listA);
    init(&listB);

    Node* ptr = list->head;
    int i = 0;
    while (ptr != NULL) {

        Node* node = create(ptr->val);
        if (i % 2 == 0)
            pushLast(&listA, node);
        else
            pushLast(&listB, node);
        i++;
        ptr = ptr->next;
    }

    puts("Input list");
    printList(list);
    puts("Odd nodes list:");
    printList(&listA);
    puts("Even nodes list:");
    printList(&listB);
}

int main(void) {

    List* list;
    init(&list);

    int i;
    for (i = 1; i <= 10; i++) {
        Node* node = create(i);
        pushLast(&list, node);
    }
    splitList(&list);
    return 0;
}

收到的输出:

Input list:
1 2 3 4 5 6 7 8 9 10
Odd nodes list:
1 3 5 7 9
Even nodes list:
2 4 6 8 10

欢迎任何帮助。

首先,您没有为指向 listlistAlistB 的指针分配内存。

其次,您将 listlistAlistB 定义为 List *。然后将 &list&listA&listB - 属于 List ** 类型 - 传递给您的函数,而您的函数期望 List *.

您需要在 splitList() 中进行以下更改。 (仅显示有错误的代码的相关部分 - 或需要添加 malloc):

void splitList(const List* list) 
{
    List *listA;
    List *listB;

    /* Allocate memory to which these pointers will point */
    if ((listA = malloc(sizeof(List))) == NULL) {
         /* Error handling code */
         exit(1);
    }
    if ((listB = malloc(sizeof(List))) == NULL) {
         /* Error handling code */
         exit(1);
    }

    /* ... */
    while (ptr != NULL)
    {
            Node* node = create(ptr->val);
            if (i % 2  ==  0)  
                    /* pushLast(&listA, node); */  /* ISSUE here */
                    pushLast(listA, node);
            else
                    /* pushLast(&listB, node); */  /* ISSUE here */
                    pushLast(listB, node);
            i++;
            ptr = ptr->next;
    }

    /* ... */

    puts("Odd nodes list:");
    /* printList(&listA); */  /* ISSUE here */
    printList(listA);
    free(ListA);  /* Free 1 */
    puts("Even nodes list:");
    /* printList(&listB); */  /* ISSUE here */
    printList(listB);
    free(ListB);  /* Free 2 */
}

此外,您需要在 main 中进行类似的更改:

int main(void)
{
    List* list;

    /* Allocate memory */
    if((list = malloc(sizeof(List))) == NULL) {
        /* Error handling code */
        exit(1);
    }

    /* init(&list); */  /* ISSUE here */
    init(list);

    int i;
    for (i = 1; i <= 10; i++)
    {
            Node* node = create(i);
            /* pushLast(&list, node); */  /* ISSUE here */
            pushLast(list, node);
    }
    /* splitList(&list); */   /* ISSUE here*/
    splitList(list);
    free(list); /* free 3 */

    return 0;
}

另请注意,您需要正确free所有使用malloc分配的内存以避免内存泄漏。可以看出,您 NOT 释放了其中的所有节点。您拥有的所有 freed 只是 one 函数中的 one 节点。