【问题标题】:C - stack implement, free memory error [duplicate]C - 堆栈工具,释放内存错误[重复]
【发布时间】:2017-11-23 18:56:16
【问题描述】:

我厌倦了使用链表来实现堆栈,所以我将其作为全局实现,并创建了一些堆栈函数(push、pop、isempty) isempty 和 push 工作得很好, 但是我遇到了弹出功能的问题,基本上它可以工作,但我不知道为什么当我尝试释放我弹出的节点的内存时(保存数据后),它不工作并导致错误。 如果我只是删除pop函数中的“free”行,它会很好用,但你知道这里的问题我必须在使用它后释放堆内存...... 那我该怎么办?

有一些代码:

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


struct stack
{
    int data;
    struct stack* next;
};
struct stack* top = NULL;  ///stack is global, so push and pop can use it.

int isEmpty()
{
    if (top == NULL)
        return 0;
    else return 1;
}

void push(int x)
{
    struct stack* temp = (struct stack*)malloc(sizeof(struct stack*));
    if (temp == NULL)
    {
        printf("Error! no allocation!!");
        return;
    }
    temp->data = x;
    temp->next = top;
    top = temp;
}
int pop()
{
    struct stack* temp;
    if (isEmpty() != 0)
    {
        temp = top;
        int x = top->data;
        top = top->next;

        free(temp);
        return x;
    }
    else
    {
        printf("stack is empty nothing to pop");
        return -1;
    }
}

int main()
{
    push(1);
    push(2);
    push(3);
    push(4);
    push(5);
    push(6);
    push(7);

    int cur;

    while (isEmpty())
    {
        cur = pop();
        printf("|%d|--->", cur);
    }

    printf("\n");
    return 0;
}

【问题讨论】:

  • struct stack* temp = (struct stack*)malloc(sizeof(struct stack*)) -- 您正在分配指向堆栈的指针的大小,而不是堆栈本身的大小。同样不赞成投射malloc 的结果。
  • struct stack *temp = malloc(sizeof *temp)
  • "栈是全局的,所以 push 和 pop 都可以使用。"这是一个非常错误的理由来拥有一个全球性的。但话又说回来,没有任何充分的理由让它全球化。
  • 您的stack 也确实是一个节点。只是说...
  • 我将 sizeof(struct stack*) 更改为 sizeof(struct stack) 现在它的工作谢谢

标签: c linked-list free allocation


【解决方案1】:

当你弹出你的 isempty() 是反向的并且你在推送时分配了指针而不是你的结构时,你的代码纠正了你的错误

为了澄清我反转了您的isempty,当它为空时它返回 0(假)是不合逻辑的

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


struct stack
{
    int data;
    struct stack* next;
};
struct stack* top = NULL;  ///stack is global, so push and pop can use it.

int isEmpty()
{
  return top == NULL;
}

void push(int x)
{
    struct stack* temp = malloc(sizeof(struct stack));
    if (temp == NULL)
    {
        printf("Error! no allocation!!");
        return;
    }
    temp->data = x;
    temp->next = top;
    top = temp;
}
int pop()
{
    struct stack* temp;
    if (!isEmpty())
    {
        temp = top;
        int x = top->data;
        top = top->next;
        free(temp);
        return x;
    }
    else
    {
        printf("stack is empty nothing to pop");
        return -1;
    }
}

int main()
{
    push(1);
    push(2);
    push(3);
    push(4);
    push(5);
    push(6);
    push(7);

    int cur;

    while (!isEmpty())
    {
        cur = pop();
        printf("|%d|--->", cur);
    }

    printf("\n");
    return 0;
}

【讨论】:

  • 谢谢伙计,我习惯了 C++,而且你在 C 中有 bool 类型,我有点生锈了,无论如何你知道如何更改我的代码,这样堆栈就不会是全局的了吗?我尝试了很多方法,但它总是粉碎......
  • 在c中一般我们在参数中给出栈。在 c++ 后面,参数中给出的堆栈是相同的,但对于开发人员堆栈是隐藏的::pop() ==> pop(stack)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 2018-11-29
  • 2016-07-25
  • 2020-04-24
  • 2011-07-25
  • 2011-08-15
  • 2011-05-23
相关资源
最近更新 更多