小编Emm*_*dez的帖子

在 C 中使用 free() 时出现堆损坏错误

今天我试图在 C 中使用链表实现一个堆栈,一切都很好,直到我创建了一个使用 free 的 pop 函数,一旦我调用 free 函数,我的程序就会崩溃,然后 Visual Studio 在新窗口中抛出堆损坏错误消息。除了这个之外,所有其他功能都工作正常,我只是不知道发生了什么。感谢您的所有回答。

这是我的代码:

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

struct Node {
    char data;
    struct Node *next;
};

void push(struct Node **top, char data) {

    struct Node *temp = (struct Node *)malloc(sizeof(struct Node *));

    if (temp == NULL) {
        printf("Stack overflow.\n");
    } else {
        temp->data = data;
        temp->next = *top;
        (*top) = temp;
    }
}

void pop(struct Node **top) {

    struct Node *aux;

    if (*top != NULL) {
        printf("Popped element: %c\n", (*top)->data); …
Run Code Online (Sandbox Code Playgroud)

c free stack linked-list heap-memory

0
推荐指数
1
解决办法
250
查看次数

标签 统计

c ×1

free ×1

heap-memory ×1

linked-list ×1

stack ×1