#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("%dn", test->top);
}
我正试图在大学作业中使用C实现一个真正基本的堆栈,并试图测试该实现,但它只给出了一个错误Segmantation fault (Core Dumped)
。我在互联网上做了一些研究,但找不到可以帮助我的东西。我知道Segmentation Fault Core Dumped
意味着我正在访问一些我不能/不应该访问的东西,但我不知道这如何适用于我的代码。
谢谢。
最好的方法是用gdb运行它,并在它segfault之后用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("%dn", test->top);
}
我所有的代码评论都引用了c程序中的堆栈内存,而不是你在这里定义的结构(这也是一个堆栈,所以有点令人困惑(。https://www.geeksforgeeks.org/memory-layout-of-c-program/