Reputation: 3
#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);
}
I am trying to implement a really basic stack using C for a college assignment and I am trying to test the implementation, but it only gives an error Segmantation fault (Core Dumped)
. I did some research on the internet, but could not find something that could help me. I understand that Segmentation Fault Core Dumped
means that I am accessing something that I can't/should not acces, but I don't know how that applies to my code.
Thanks.
Upvotes: 0
Views: 270
Reputation: 685
The best thing to do is to run it with gdb and get a stack track with the bt cmd after it segfaults. I have added some comments to your initial code that may or may not fix your problem, but main was clearly broken. "test" in main, was never assigned to anything so test->top is an attempt to dereference whatever address was assigned to "test", which could be any garbage value that is in "c program stack's memory" at the time. Make sure to initialize your variables before you use them.
#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);
}
All of my code review comments that reference stack refer to stack memory in a c program, not the structure you are defining here (which is also a stack so its kinda confusing). https://www.geeksforgeeks.org/memory-layout-of-c-program/
Upvotes: 0