2

i'm studying a data structure. Now i have tried to write some stack by language C, unfortunately unsuccessfully.

In case 4 in switch statement i have an error like this: Exception thrown: read access violation.

What there can be an error?

Here is my code:

int main(int argc, char** argv)
{
    int size;
    int element;

    printf("enter the size of stack: ");
    scanf("%d", &size);

    uint32_t* arr = malloc(sizeof(uint32_t) * size);
    stack_t structure= { arr, size, 0};

    int check = 1;

    while (check == 1)
    {
        int op;
        int condition;

        printf("Enter the action: 1) check stack for empty; 2) push function; 3) pop function; 4) print stack\n");
        printf("Command: ");
        scanf("%d", &op);
        printf("\n");

        switch (op)
        {
        case 1:
            structure.empty = stack_empty(&structure);
            break;

        case 2:
            printf("enter the number to be pushed: ");
            scanf("%d", &element);
            push(&structure, element);
            break;

        case 3:
            structure.stack = pop(&structure);
            break;

        case 4:
            for (int i = 0; i < structure.size; i++)
                printf("[%d] = %d\n", i, structure.stack[i]);


            break;
        default:
            printf("The command is not found, try again\n");
            break;
        }

        printf("Continue? y/n(1/0)\n");
        scanf("%d", &condition);

        check = condition;
    }

    system("pause");
    return 0;
}

Here is functions who's probably may not work correctly

typedef struct stack
{
    uint32_t* stack;
    int size;
    uint8_t top;
    uint8_t empty;
}stack_t;


int stack_empty(stack_t* s)
{
    if (s->top == 0)
        return 1;
    else
        return 0;
}

void push(stack_t* s, uint8_t element)
{
    s->top = s->top + 1;
    s->stack[s->top] = element;
}


int pop(stack_t* s)
{
    if (stack_empty(s))
    {
        printf("stack is underflow\n");
        exit(1);
    }
    else
    {
        s->top = s->top - 1;
        return (s->stack[s->top + 1]);
    }
}
9
  • 7
    This is very wrong: (uint32_t)malloc(...) Your compiler should have given you a strong warning/error about assigning an integer type to a pointer. Don't ignore those warnings. Fix them before posting here. In this case, the correct cast would be (uint32_t *), but you should just get rid of the cast entirely, it's not needed. But by casting the pointer to an integer, if your pointer is larger than 32 bits, it is destroyed by the cast. Commented Apr 27, 2020 at 9:48
  • if (stack_empty(&s)) should be if (stack_empty(s)). s is already a pointer, so you want to pass its value, not its address. Commented Apr 27, 2020 at 9:52
  • the fields element, underflow, overflow and empty should not be part of the stack structure. Commented Apr 27, 2020 at 9:52
  • 1
    @Gerhardh i apologize for that Commented Apr 27, 2020 at 10:24
  • 1
    @DimaRich yes thanks I thought about it myself Commented Apr 27, 2020 at 10:26

1 Answer 1

1

First, the structure is initialized incorrectly. It must be

stack_t structure = { arr, size, 0, 1};

because the stack is empty at program start.

Second, the push function does things in wrong order. It must be

void push(stack_t* s, uint8_t element)
{
    s->stack[s->top] = element;
    s->top = s->top + 1;
}

because we want to start pushing at pos 0.

Third, the pop function does also things in wrong order. It must be

...
else
    {
        s->top = s->top - 1;
        return (s->stack[s->top]);
    }

because the push function increases the stack ptr after pushing the element and when we want to pull an element we must decrease the stack pointer first and then get the element at this position.

Fourth, the case 3 is wrong. It must be

element = pop(&structure);

Fifth, the for loop for printing elements has a wrong boundary check. Use:

for (int i = 0; i < structure.top; i++)
                printf("[%d] = %d\n", i, structure.stack[i]);

Regarding those issues it should work if not pushing more than 10 elements, because the sanity check for the upper bound is missing, too.

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome. I maybe hit a rough tone of voice by using "wrong" and "it must be" several times. Wasn't meant so. Of course there are always several ways to do things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.