分段故障(核心已转储),但找不到故障

时间:2018-10-31 16:58:14

标签: c segmentation-fault stack

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

struct stack {
    // ... SOME CODE MISSING HERE ...
    int top;
    int stackArray[STACK_SIZE];
    int push;
    int pop;
};

struct stack *stack_init() {
    struct stack* s = (struct stack*) malloc(sizeof(struct stack));
    s->top = 0;

    if (s == NULL)
        return NULL;
    else
        return s;
}

void stack_cleanup(struct stack* s) {

    for(int i = 0; i < STACK_SIZE; i++)
        s->stackArray[i] = 0;

    free(s);
}

int stack_push(struct stack *s, int c) {

    if (s->top <= STACK_SIZE){
        s->stackArray[s->top] = c;
        s->top++;
        s->push++;
        return 0;
    }
    else
        return 1;


}

int stack_pop(struct stack *s) {

    if (!stack_empty(s)){
        return s->stackArray[s->top];
        s->top--;
        s->pop++;
    }
    else
        return -1;
}

int stack_peek(struct stack *s) {

    if (!stack_empty(s)){
        return s->stackArray[s->top];
    }
    else
        return -1;
}

int stack_empty(struct stack *s) {

    if (s->top == -1)
        return 1;
    else
        return 0;
}

int main(){
    struct stack *test;
    // stack_peek(test);
    // return 0;
    printf("%d\n", test->top);
}

我正在尝试使用C来实现一个用于大学分配的真正基本的堆栈,并且正在尝试测试该实现,但是它仅给出错误Segmantation fault (Core Dumped)。我在互联网上做了一些研究,但是找不到可以帮助我的东西。我了解Segmentation Fault Core Dumped表示我正在访问某些我无法/不应访问的内容,但是我不知道这对我的代码有何作用。

谢谢。

1 个答案:

答案 0 :(得分:0)

最好的办法是使用gdb运行它,并在segfaults之后使用bt cmd获得堆栈跟踪。我已经在您的初始代码中添加了一些注释,这些注释可能会或可能不会解决您的问题,但是main显然已损坏。 main中的“ test”从未分配任何内容,因此test-> top尝试取消引用分配给“ test”的任何地址,该地址可能是当时“ c程序堆栈的内存”中的任何垃圾值。使用变量之前,请确保对其进行初始化。

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

struct stack {
    // ... SOME CODE MISSING HERE ...
    int top;
    int stackArray[STACK_SIZE];
    int push;
    int pop;
};

struct stack *stack_init() {
    struct stack* s = (struct stack*) malloc(sizeof(struct stack));
    s->top = 0; // HERE IS A POTENTIAL NULL POINTER DEREFENCE, MOVE THIS AFTER THE NULL CHECK

    if (s == NULL)
        return NULL;
    else
        //HERE IS WHERE THE ABOVE LINE SHOULD BE LOCATED. s->top = 0;
        //WHAT ABOUT THE REST OF THE MEMORY IN THIS STRUCTURE, SHOULD IT BE 0?
        return s;
}

void stack_cleanup(struct stack* s) {

    for(int i = 0; i < STACK_SIZE; i++)
        s->stackArray[i] = 0;

    free(s);
}

int stack_push(struct stack *s, int c) {

    if (s->top <= STACK_SIZE){ //CAN s->top EVER BE NEGATIVE?
        s->stackArray[s->top] = c; //BECAUSE THIS WOULD BE A BAD INDEX IF IT WERE NEGATIVE HERE
        s->top++;
        s->push++;
        return 0;
    }
    else
        return 1;


}

int stack_pop(struct stack *s) {

    if (!stack_empty(s)){
        return s->stackArray[s->top];
        s->top--;//THIS LOOKS LIKE IT WILL NEVER GET EXECUTED
        s->pop++;//SAME WITH THIS LINE, MOVE THEM ABOVE THE RETURN STATEMENT IF YOU WANT THEM TO BE EXECUTED.
    }
    else
        return -1;
}

int stack_peek(struct stack *s) {

    if (!stack_empty(s)){
        return s->stackArray[s->top];
    }
    else
        return -1;
}

int stack_empty(struct stack *s) {

    if (s->top == -1)
        return 1;
    else
        return 0;
}

int main(){
    struct stack *test; //EITHER MALLOC MEMORY FOR THIS GUY OR PUT IT ON THE STACK (stack vs heap memory)
    //SAMPLE STACK USAGE, DOESNT USE YOUR INIT FUNCTION
    struct stack test;
    memset(test, 0, sizeof test);

    //IF YOU DONT WANT TO MALLOC YOU CAN POINT IT TO THE STACK VERSION ABOVE
    struct stack *test2 = &test; //NOW YOU DONT NEED MALLOC, BUT YOU STILL DIDNT USE YOUR SPECIAL INIT.
    // stack_peek(test);
    // return 0;
    printf("%d\n", test->top);
}

我所有的代码回顾注释都引用了堆栈,它们引用了c程序中的堆栈内存,而不是您在此处定义的结构(它也是一个堆栈,因此有点令人困惑)。 https://www.geeksforgeeks.org/memory-layout-of-c-program/