【问题标题】:Dynamic stack in C [pop crash] [closed]C中的动态堆栈[pop crash] [关闭]
【发布时间】:2015-04-07 18:27:23
【问题描述】:

我正在制作一个使用动态堆栈将十进制整数转换为二进制的程序。 它在最后一次弹出时崩溃。 前任。数量:4 输出:10 崩溃

#include <stdio.h>

struct stack {
    struct stack *prev;
    int val;
    struct stack *next;
};
struct stack *first,*cur,*tmp;
struct stack *GETNODE(){
    struct stack *pt = (struct stack *)malloc(sizeof(struct stack));
};
int counter=1;
void push(int val){
    tmp=GETNODE();
    tmp->prev=NULL;
    tmp->val=val;
    tmp->next=NULL;
    if(first==NULL){
        first=tmp;
        cur=first;
    }else{
        tmp->prev=cur;
        cur->next=tmp;
        cur=tmp;
    }
    counter++;
};

int pop(){
    int val=tmp->val;
    cur=tmp->prev;
    free(tmp);
    tmp=cur;
    tmp->next=NULL;
    counter--;
    return(val);
};


main(){
    int num = 4;
    while(num!=0){
        push(num%2);
        num/=2;
    }
    while(counter!=1){
        printf("%d ",pop());
    }
}

【问题讨论】:

  • MSVC 编译器警告 C4716:'GETNODE' : must return a value.
  • @JonathonReinhart 老师告诉我们这样做的,而且这似乎不是问题
  • 也请告诉你的老师#include &lt;stdlib.h&gt;
  • 如果您的老师真的认为演员表可以替代正确的声明,您需要一位新老师。告诉他们。

标签: c dynamic stack


【解决方案1】:

问题出在你的 pop 函数中。如果您考虑它是如何运行的,在最后的传递中您将 free(tmp),它当前指向第一个项目。释放后,然后分配:

tmp->next=NULL;

此时您正试图取消对无效指针的引用。

【讨论】:

  • 哎呀,谢谢!我永远不会注意到
  • 如果您使用调试器单步调试代码,您注意到这一点。
【解决方案2】:

我对您的代码做了很多更改。主要是,它太复杂了——只需要一个单链表,你不需要计数器——只需跟踪列表指针。您的 GETNODE() 函数没有返回值,导致调用函数中出现未定义的行为。我也简化了,不需要单独的函数来分配内存,因为malloc() 已经这样做了。

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

struct stack {
    struct stack *prev;
    int val;
};

struct stack *first = NULL;

void push(int val){
    struct stack *pt = malloc(sizeof(struct stack));
    if (pt == NULL){
        printf ("Bad call to malloc()\n");
        exit (1);
    }
    pt->prev=first;
    pt->val=val;
    first = pt;
}

int pop(){
    int val;
    struct stack *pt = first;
    if (first == NULL)
        return -1;
    val=first->val;
    first = first->prev;
    free(pt);
    return(val);
}

void dec2bin(int num){
    printf("%-5d", num);
    while(num!=0){
        push(num%2);
        num/=2;
    }
    while(first){
        printf("%d",pop());
    }
    printf("\n");
}

int main(void){
    dec2bin (4);
    dec2bin (129);
    dec2bin (160);
    return 0;
}

程序输出:

4    100
129  10000001
160  10100000

【讨论】:

    【解决方案3】:

    我更改了您的部分代码,您的代码现在可以正常工作了。

     #include <stdio.h>
     #include <stdlib.h>
    
    struct stack {
         struct stack *prev;
         int val;
         struct stack *next;
    };
    
    struct stack *first, *cur, *tmp;
    int counter = 0;
    
    struct stack *GETNODE()
    {
         return malloc(sizeof(struct stack));
    }
    
    
    void push(int val)
    {
        tmp = GETNODE();
        tmp->prev = NULL;
        tmp->val = val;
        tmp->next = NULL;
    
        if (first == NULL) {
             first = tmp;
             cur = first;
        } else {
             tmp->prev = cur;
             cur->next = tmp;
             cur = tmp;
        }
        counter++;
    }
    
    int pop(void)
    {
         int val = cur->val;
    
         tmp = cur;
         cur = cur->prev;
         free(tmp);
         counter--;
    
         return val;
     }
    
    
    int main(int argc, char *argv[])
    {
        int num;
    
        scanf("%d", &num);
    
        while (num != 0) {
            push(num % 2);
            num /= 2;
        }
        while (counter != 0)
        printf("%d ", pop());
        printf("\n");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-24
      • 2023-01-11
      • 1970-01-01
      • 2013-05-26
      • 2013-08-27
      • 2013-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多