'Different output while using node and node pointer in structure

I am writing code to create a stack and know whether the stack is empty or not.

#include <stdio.h>
#include <stdlib.h>

struct stack
{
    /* data */
    int size;
    int top;
    int *arr;
};

// Functions
int isEmpty(struct stack ptr)
{
    if (ptr.top == -1)
    {
        /* code */
        return 1;
    }
    else
    {
        return 0;
    }
}

int isFull(struct stack ptr)
{
    if (ptr.top == ptr.size - 1)
    {
        /* code */
        return 1;
    }
    else
    {
        return 0;
    }
}

int main()
{

    struct stack s; // Stack is used as pointer for ease.
    s.size = 80;
    s.top = -1;
    s.arr = (int *)malloc(s.size * sizeof(int));

    // Pushing an element manually
    s.arr[0] = 7;
    s.top++;

    if (isEmpty(s))
    {
        /* code */
        printf("The stack is empty.\n");
    }
    else{
        printf("The stack is not empty.\n");
    }
    
    if (isFull(s))
    {
        /* code */
        printf("The stack is full.\n");
    }
    else{
        printf("The stack is not full.\n");
    }

    return 0;
}

This code goes very well. But when I use Node *(pointer) for the stack, program doesn't execute completely.

#include <stdio.h>
#include <stdlib.h>

struct stack
{
    /* data */
    int size;
    int top;
    int *arr;
};

// Functions
int isEmpty(struct stack *ptr)
{
    if (ptr->top == -1)
    {
        /* code */
        return 1;
    }
    else
    {
        return 0;
    }
}

int isFull(struct stack *ptr)
{
    if (ptr->top == ptr->size - 1)
    {
        /* code */
        return 1;
    }
    else
    {
        return 0;
    }
}

int main()
{

    struct stack *s; // Stack is used as pointer for ease.
    s->size = 8;
    s->top = -1;
    s->arr = (int *)malloc(s->size * sizeof(int));

    // Pushing an element manually
    // s->arr[0] = 7;
    // s->top++;

    if (isEmpty(s))
    {
        /* code */
        printf("The stack is empty.");
    }
    else{
        printf("The stack is not empty.");
    }

    return 0;
}

Sometimes the code doesn't even run. And.....

Sometimes this shows an error like this:

    c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe:

cannot open output file Creation.exe: Permission denied collect2.exe: error: ld returned 1 exit status

Please help!!



Solution 1:[1]

The problem in the 2nd code snippet is that s is uninitialized meaning it is not pointing to any object and you're dereferencing it when you wrote s->size, s->top and s->arr, which leads to undefined behavior.

Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the output of a program that has undefined behavior.

So the output that you're seeing(maybe seeing) is a result of undefined behavior. And as i said don't rely on the output of a program that has UB. The program may just crash.

For example, here the program doesn't crash but here it crashes.

So the first step to make the program correct would be to remove UB. Then and only then you can start reasoning about the output of the program.

To solve this make sure that s points to a stack object before dereferencing it(s).


1For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1