Sidward Sidward - 1 month ago 17
C Question

Unable to initialize a stack size to 0

I'm a little rusty on using stacks so there might be something obviously wrong here. I'm either getting a segfault when I try to set my stack size to 0 I'm getting a segfault in my push function when the first if statement executes. The code below will segfault in the first line of push function.

typedef strcut Stack{
Node data[UNIT_MAX];
int size;
} Stack;

//
Stack* DFS(Node* G, int numbVertices, Node v){
//...More code above
Stack* S = NULL;

//Segfaulting when I try to set S->size = 0!!!

push(S,v);


}

//
void push(Stack* S, Node d){
if(S->size < UNIT_MAX){
S->data[S->size++] = d;
}
else
exit(STACK_FULL);
}

Answer
Stack* S = NULL;

//Segfaulting when I try to set S->size = 0!!!

Certainly your program crash because you set S is NULL.

(Stack*)(NULL)->size = 0. It is sure to crash because you access the NULL address.