user5948200
user5948200

Reputation:

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);
}

Upvotes: 0

Views: 52

Answers (1)

linuxer
linuxer

Reputation: 426

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.

Upvotes: 1

Related Questions