0

I was trying to create a Stack using struct in C as this:

typedef struct 
{
    int index;
    int* stack;
} Stack;

and a function to allocate memory for it:

Stack* getNewStack(void) // function to get a new stack
{
    Stack *temp = (Stack*)malloc(sizeof(Stack));    // allocating memory for struct 

    if (temp == NULL)
    {
        printf("Error : Memory allocation failed!\n");
        return NULL;
    }

    temp->stack = (int*)malloc(sizeof(int) * 5);    // allocating memory for pointer

    if (temp->stack == NULL)
    {
        printf("Error : Memory allocation failed!\n");
        return NULL;
    }

    temp->index = 0;
    temp->stack[0] = 0; // init it to 0
    
    return temp;
}

push function:

void push(Stack* stk, const int value)  // function to push a value in stack
{
    if (stackNull(stk)) // checks stk == NULL
        return;
    
    if (stk->index + 1 == sizeof(stk->stack)/sizeof(stk->stack[0]))
        stk->stack = (int*)realloc(stk->stack, sizeof(int) * (stk->index + 10));
    // in case stack gets full, it will be increased by 10 

    stk->stack[stk->index++] = value;
}

in main function:

int main()
{
    Stack* new = getNewStack();
    
    for (int i = 0; i < 30; ++i)
        push (new, i + 1);

    for (int i = 0; i < 30; ++i)
        printf("%d ", new->stack[i]);
    printf("\n");

    return 0;
}

compile and run using:

gcc test.c -o test && ./test

output:

malloc(): corrupted top size
Aborted (core dumped)

I don't understand why, Surprisingly enough, if i simply just push, and don't print anything, it compiles and executes successfully. Moreover, if i add printf(" "); in the function getNewStack, like this :

Stack* getNewStack(void) // function to get a new stack
{
    printf(" ");
    Stack *temp = (Stack*)malloc(sizeof(Stack));    // allocating memory for struct 
    // ...
    // rest code same as above 
}

it somehow works and prints out the desired output.

What am i missing or doing wrong ?

1 Answers1

0

You need to track the size of the allocated memory yourself. sizeof(stk->stack) is not the size of the allocated memory pointed by stk->stack.

This is your modified code, check the comments containing <<< :

Stack * getNewStack(void) // function to get a new stack
{
  Stack* temp = (Stack*)malloc(sizeof(Stack));

  if (temp == NULL)
  {
    printf("Error : Memory allocation failed!\n");
    return NULL;
  }

  temp->size = 5;                                          // <<< initial size
  temp->stack = (int*)malloc(sizeof(int) * temp->size);    // <<<

  if (temp->stack == NULL)
  {
    printf("Error : Memory allocation failed!\n");
    return NULL;
  }

  temp->index = 0;

  // temp->stack[0] = 0; // << this is useless

  return temp;
}


void push(Stack * stk, const int value)  // function to push a value in stack
{
  if (stk->index == stk->size)
  {
    stk->size += 10;              // <<< stack full => increment size by 10
    stk->stack = realloc(stk->stack, sizeof(int) * stk->size);  // <<<
  }

  stk->stack[stk->index++] = value;
}

There is still room for further improvement.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115