0

I'm trying to implement a stack in C. I have only implemented the struct that will contain an array and that currently only contains the size of the array and the position of the last item added to the stack

This is a partial implementation that is giving me some trouble.

in stack.h

#include <stdlib.h>
#include <stdbool.h>

typedef struct Stack
{
    int max_size;
    int top;
    // int *contents;
} Stack;

Stack *stack_create(int n);
bool stack_is_empty(Stack *stack);
bool stack_is_full(Stack *stack);
void stack_push(Stack *stack, int value);

in stack.c:

#include <stdio.h>
#ifndef STACK_H
#include "stack.h"
#endif

Stack *stack_create(int n)
{
    Stack stack;
    Stack *s = &stack;
    s->max_size = n;
    s->top = 0;
    // s->contents = (int *)malloc(sizeof(int) * n);
    return s;
}


bool stack_is_empty(Stack *stack)
{
    if (stack->top == 0)
    {
        return true;
    }
    return false;
}

bool stack_is_full(Stack *stack)
{
    if (stack->top == stack->max_size)
    {
         return true;
    }
    return false;
} 

void stack_push(Stack *stack, int value)
{

     if (!stack_is_full(stack))
     {
          printf("max_size: %d\n", stack->max_size);
          printf("top: %d (%p)\n", stack->top++, &stack->top);
          printf("value: %d (%p)\n", value, &value);
     }
     else
     {
          printf("Can't push. max_size==%d reached.\n", stack- >max_size);
          exit(EXIT_FAILURE);
     }
}

and in main.c:

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

 #define SIZE 3

 int main()
 {
     Stack *s = stack_create(SIZE);
     printf("stack_is_empty: %d\n", stack_is_empty(s));
     stack_push(s, 100);
     printf("stack_is_empty: %d\n", stack_is_empty(s));
     stack_push(s, 30);
     printf("stack_is_empty: %d\n", stack_is_empty(s));
     stack_push(s, 20);
     printf("stack_is_empty: %d\n", stack_is_empty(s));

     return 0;
 }

main produces the following output:

stack_is_empty: 1
max_size: 3
top: 100 (0x7ffd5430dfb4)
value: 101 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 30 (0x7ffd5430dfb4)
value: 31 (0x7ffd5430dfb4)
stack_is_empty: 0
max_size: 3
top: 20 (0x7ffd5430dfb4)
value: 21 (0x7ffd5430dfb4)
stack_is_empty: 0

Why is value's address the same of stack->top?

2
  • You need to learn about dynamic allocation and how to use malloc (and free). The pointer that stack_create is a pointer to a local variable, whose life-time ends when the function returns. Commented Nov 24, 2018 at 16:18
  • 1
    In stack_push you do a lot of nice printing, but you don't actually put the value in the stack: stack->content[stack->top++]= value; Commented Nov 24, 2018 at 16:38

1 Answer 1

3

Problem 1 : You are allocating memory for the stack locally in stack_create function. As soon as the function goes out of scope memory will be freed. Thus you will have a dangling pointer.

Problem 2 : You are allocating memory only for one instance regardless of value of 'n'

typedef struct Stack
{
    int max_size;
    int *contents;
    int top;
    // int *contents;
} Stack;

Stack *stack_create(int n) {
    Stack *s;
    s = (Stack *)malloc(sizeof(Stack));
    s->contents = (int *)malloc(sizeof(int) * n);
    s->max_size = n;
    s->top = 0;
    return s;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Don't cast the fesult of malloc. void * is compatible with all pointer types.
Problem 3: you don't actualy push anything. Add: stack->content[stack->top++]= value;
@PaulOgilvie I knew that. I removed the push function because I was having trouble with all the rest!

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.