user17441147
user17441147

Reputation:

Why is my program not reallocating memory in C?

I'm working on this program that creates a stack, pushes and pops values then deletes the stack and deallocates the memory. What I want the function stack_push to do is push values to the stack and if the stack is full, it doubles the amount of memory it has, basically reallocating the memory and doubling it. In this case, it should go from 5 variables to 10. Yet for some reason, it's not doing that. I believe the error stems from my attempt at reallocating memory, what am I doing wrong and how would I go about fixing it?

typedef struct stack
{
    int capacity;
    int size;
    double *data;
} Stack;

Stack *ptr;
Stack *stack_create(void){
  ptr = (Stack*)malloc(sizeof(Stack));
  ptr->capacity = 5;
  ptr->size = -1;
  ptr->data = (double*)malloc(sizeof(double) * ptr->capacity);

  return ptr;

}

void stack_push(Stack *s, double value){

  if (s->size >= s->capacity-1){
    ptr = (Stack *)realloc(ptr, 2*sizeof(Stack));
  };

  ptr->data[++ptr->size] = value;

}

int main(void)
{
    // Create an empty stack.
    Stack *s = stack_create();
        for (int i = 0; i < 10; i++) {
        stack_push(s, i);
    }
    return 0;
}

Upvotes: 0

Views: 89

Answers (2)

autistic
autistic

Reputation: 15642

This post highlights the dangers of not reading manuals, FAQs and textbooks when learning. I suggest picking up a copy of K&R2e and doing the exercises as you stumble across them.

ptr = (Stack *)realloc(ptr, 2*sizeof(Stack));

For a start, you're working in C, not C++; you shouldn't be casting the return value of realloc. That's probably not going to cause any headaches beyond what you'd expect from boilerplate crud, but what will cause headaches is if you don't realise the value of ptr may change, and that change will only be local to stack_push because of pass-by-value semantics. Additionally, when ptr does change (but not for the caller), realloc has invalidated the old value, which leads the caller somewhere into this line of frequently asked questions...

See what I mean? Just from this one line of code I can see that your textbooks, college classes and whatnot aren't working out well. Something needs to change. Please 🙏 do consider reading K&R2e and doing those exercises when you can.

Upvotes: 0

dbush
dbush

Reputation: 223972

You're reallocating an additional stack, not more elements in the stack:

ptr = (Stack *)realloc(ptr, 2*sizeof(Stack));

What you want instead is:

ptr->data = realloc(ptr->data, 2 * ptr->capacity * sizeof(double));
if (!ptr->data) {
    perror("malloc failed");
    exit(1);
}
ptr->capacity *= 2;

The last line keeps track of the updated capacity so that you'll know when you need to reallocate again.

Note that you should always check the return value of malloc and realloc to ensure that the memory was successfully allocated, and that you shouldn't cast the return value as that can mask other errors in your code.

Upvotes: 1

Related Questions