Stack in C with error "Process returned -1073741819 (0xC0000005)"

0
#include <stdio.h>
#include <stdlib.h>
#define N 5

typedef struct stack{
    int top;
    int info[N];
} stack;

void initStack(stack *s);
void push(stack *s, int val);
void pop(stack *s);
int isFull(stack s);
int isEmpty(stack s);
void printStack(stack *s);

int main(){
    stack *s;
    initStack(&s);
    push(&s, 19);
    push(&s, 2);
    push(&s, 19);
    push(&s, 2);
    push(&s, 19);
    printStack(&s);

    return 0;
}

void initStack(stack *s){
    s->top = -1;
}

void push(stack *s, int val){
    if(isFull(*s) == 0){
        s->top++;
        s->info[s->top] = val;
    } else printf("Stack full!\n");
}

void pop(stack *s){
    if(isEmpty(*s) == 0) s->top--;
    else printf("Stack empty!\n");
}

int isFull(stack s){
    if(s.top == N-1) return 1;
    else return 0;
}

int isEmpty(stack s){
    if(s.top == -1) return 1;
    else return 0;
}

void printStack(stack *s){
    for(int i = 0; i < s->top + 1 && i < N; i++)
        printf("%d ", s->info[i]);
    printf("\n");
}

I have problems with this code... i think i'm writing out of the stack (index of bound) even if, teorically, I should have positions free. I got the error:

Process returned -1073741819 (0xC0000005)

c
pointers
struct
stack
asked on Stack Overflow Apr 19, 2020 by davide • edited Apr 19, 2020 by Jonathan Wood

2 Answers

1

you define

void initStack(stack *s){

but you call

stack *s;
initStack(&s);

you define

void push(stack *s, int val){

but you call

stack *s;
...
push(&s, 19);
push(&s, 2);
push(&s, 19);
push(&s, 2);
push(&s, 19);

you define

void printStack(stack *s){

but you call

 stack *s;
 ....
 printStack(&s);

All your calls are invalid

Replace

stack *s;

by

 stack s;

after that, compilation and execution:

pi@raspberrypi:/tmp $ gcc -Wall c.c
pi@raspberrypi:/tmp $ ./a.out
19 2 19 2 19 
pi@raspberrypi:/tmp $ 

No messages was produced by your compiler when you compiled your version ?


You do not use a pointer for isFull and isEmpty(stack s), you will have the expected result (supposing you really give a stack in argument and not a pointer to a stack or something else), but doing that the stack is copied to fill the parameter, this is more expensive than to just get a pointer, so I recommend you to also get a pointer to a stack rather than a stack and you can also simplify their definition :

int isFull(stack * s);
int isEmpty(stack * s);

...

int isFull(stack * s) {
    return (s->top == N-1);
}

int isEmpty(stack * s) {
    return (s.top == -1);
}

To come back to your initial problem, having :

stack s;
stack * p;
stack ** pp;
  • s is a stack, that means a block of memory able to memorize the N+1 int
  • p is a pointer to a stack, that means p can memorize the address of a stack, but that pointer is only useful in case it points to an existing stack
  • pp is a pointer to a pointer to a stack (a double pointer), that means pp can memorize the address of a pointer to a stack, but again pp is only useful in case it points to an existing pointer to a stack.

adding initialization :

stack s;
stack * p = &s;
stack ** pp = &p;

s.top = -1;

we have :

  • s.top == -1
  • because p point to s we have *p == s and p->top == -1 (note that p->top and (*p).top are two ways to write exactly the same thing, the "->" is a shortcut making the code more readable)
  • because pp point to p we have *pp == p, and then **pp == s and (*pp)->top == -1

In your code a call like initStack(&s); gives the address of s where s values a non initialized pointer to a stack, but for initStack its argument is a pointer to a valid stack and not a pointer to a non initialized pointer to a stack => s->top = -1; cannot work.

A valid code where s is a pointer to a stack and giving &s in argument to initStack need to be something like that :

void initStack(stack ** s){
 (*s)->top = -1;
}

...

stack st;
stack * s = &st;

initStack(&s);

but of course this is too complicated for nothing, compare with the code I already given

answered on Stack Overflow Apr 19, 2020 by bruno • edited Apr 20, 2020 by bruno
1

You're passing the address of a stack pointer s to stack functions like initStack (i.e. the type passed to initStack, push, etc. is stack **). Changing the declaration of s inside main() (and only main(), not the other functions) to stack s will solve your trouble.

If you can find your compiler's warning settings, turn them on. They will tell you when something like this type mismatch issue occurs.

answered on Stack Overflow Apr 19, 2020 by MemReflect • edited Apr 20, 2020 by MemReflect

User contributions licensed under CC BY-SA 3.0