I'm experiencing some trouble with an assignment. I am supposed to implement a dynamically growing stack that doubles its size when it's full and halves it when it's 1/4 full. Since I am a total C beginner and are unfamiliar with pointers, I've looked through some examples and this is the Code I came up with.
It actually compiles in gcc without warnings, but produces a "Segmentation fault" when I try to run it. I found out that this probably has to do with broken pointers, but I don't see any mistakes and would be glad if someone could point it out for me.
Cheers
# ifndef STACK_H
# define STACK_H
# include "stdlib.h"
typedef struct stack {
int *stack;
int used;
int size;
} stack;
stack* stck_construct() {
stack *stck;
stck->stack = (int *)malloc(10 * sizeof(int));
stck->used = 0;
stck->size = 10;
return stck;
}
void stck_destruct(stack *stck) {
stck->stack = 0;
stck->used = stck->size = 0;
free(stck);
}
int stck_push(stack *stck, int val) {
if (stck->used == stck->size) {
stck->size *= 2;
stck->stack = (int *)realloc(stck->stack, stck->size * sizeof(int));
}
stck->stack[stck->used] = val;
stck->used++;
return 1;
}
int stck_pop(stack *stck, int *val) {
*val = stck->stack[stck->used];
free(stck->stack);
stck->used--;
if (stck->used <= (stck->size)/4) {
if (stck->size <=40) stck->size = 10;
else stck->size /= 2;
stck->stack = (int *)realloc(stck->stack, stck->size * sizeof(int));
}
return 1;
}
int main(){
stack* test;
test=stck_construct();
int i; int out;
for (i =1; i<=10; i++)
stck_push(test, i);
for (i =1; i<=10; i++) {
stck_pop(test,&out);
printf("%i\n", out);
}
stck_destruct(test);
return 0;
}
# endif