views:

103

answers:

4

Hi,

when I'm trying to push elements to a stack I get segmentation fault, but if I open address for stack(i marked them with "!!!") and it's symbols it accepts it. But this time in each push, it creates new address and doesn't increase top value.

typedef struct
{
  struct table **symbols; // array of the stack
  int top; //index of the top element
  int size; //maximum size of the stack
}stack;

void push(stack *stck,struct table *element)
{  
    if(stck->top == stck->size)
    {
      printf("stack is full");
      return;
    }

    stck = malloc(sizeof(stack));                                          !!!
    stck->symbols = (struct table **)malloc(50 * sizeof(struct table*));   !!!

    printf("top : %d\n",stck->top);
    stck->top = stck->top++;
    printf("%d"&stck->top);
    stck->symbols[stck->top] = element;
    printf("top : %d\n",stck->top);
}
+1  A: 

You are passing one stack in a variable called stck, but then allocating a new structure and assigning it to the same pointer. So the stack you pass in is never modified. I don't think you need the first malloc call.

AShelly
yes but when I remove that it gives segmentation fault : ) I'm asking how to remove that
iva123
+2  A: 

You have to construct your stack before you can push anything onto it. Eg. create function stack_new that will allocate memory for your stack and initialize its members:

stack * stack_new (size_t size)
{
    stack * stck = malloc(sizeof(stack));
    stck->top = -1;
    stck->size = size
    stck->symbols = (struct table **)malloc(size * sizeof(struct table*));
    return stck;
}

Now, once you properly constructed your stack with above function, you may pass it to push function.

el.pescado
you are the new king : ) I forget to return value
iva123
+1  A: 

Before you ever call push, you are going to want to malloc space for the stack.

With the !!! lines, you are allocating new memory with each push, which is wrong.

Without the !!! lines, you are never allocating the memory

Shmoopty
A: 

If you pass a single pointer stack *stck and then malloc inside the function, it will not reflect once you get out the function.

Also, why do you need to allocate memory for table for 50 pointers each time you want to push?

Do it like below:

struct table
{
   //members go here
};

typedef struct
{
  struct table **symbols;
  int top;
  int size;
}stack;


struct table *get_elem(void)
{
   //logic for getting elements go here
}

void stack_push(stack *stck, struct table *element)
{

  if(stck->top==stck->size)
  {
    printf("Stack Full\n");
    return;
  }

  stck->top++;
  stck->symbols[stck->top] = element;
  return;
}

void stack_func()
{

 struct table *elem = NULL;
 stack *stck = (stack *)malloc(sizeof(stack));
 if(NULL==stck)
 {
   return;
 }

 stck->top = -1;
 stck->symbols = (struct table **)malloc(50 * sizeof(struct table *));

 if(NULL == stck->symbols)
 {
   free(stck);
   return;
 }

  stck->size = 49;

  elem = get_elem();
  //do check here for correctness of elem returned from get_elem

  stack_push(stck, elem);

  return;
}

Hope this helps! : )

Jay