2

I'm trying to implement a stack structure in C, for storing char arrays into.

I have the following code:

typedef struct {
  size_t size;
  char **data;
} loods1;

loods1 *init(void) {
  loods1 *loods = malloc(sizeof(loods1));
  loods->data = malloc(sizeof(char *) * STACK_MAX);
  for (int i = 0; i < STACK_MAX; i++) {
    *(loods->data + i) = malloc(LABEL_LENGTH_MAX * sizeof(char));
  }   
  loods->size = 0;
  if (loods == NULL) {
    perror("malloc failed\n");
    return NULL;
  }
  return loods;
}

int empty(loods1 *loods) {
  return (loods->size == 0);
}
void push(loods1 *loods, char *name) {
  if (loods->size == STACK_MAX) {
    perror("Stack is full\n");
    exit(0);
  }
  else {
    *((loods->data) + loods->size++) = name;
  }
}
char *pop(loods1 *loods) {
  if (loods->size == 0) {
    printf("size == 0\n");
    return NULL;
  }
  else {
    printf("%s \n", *(loods->data + 1));
    return *(loods->data + (--loods->size));
  }
}
int delete(loods1 *loods) {
  for (int i = 0; i < STACK_MAX; i++) {
    free(*(loods->data + i));
  }   
  free(loods->data);
  free(loods);   
}

There are 2 problems: first off, every time I add a new element to the stack, it overwrites all existing elements (if '3' and '11' are added and I want to add '15', the new stack will look like '15', '15', '15'). And when I want to pop the stack, the popped value is empty. Not null, but an empty string or something?

I have no idea what I'm doing wrong, but there seems to be a mistake somewhere, obviously.

Sammy

1
  • 1
    You're not showing where you're calling this code, but my guess is that you've got name in a static buffer or something? If you want the actual strings to go on the stack, you need to allocate memory for each before you put it there. Alternatively, you can do *((loods->data) + loods->size++) = strdup(name). You just have to free them individually later. Commented Mar 2, 2012 at 19:21

2 Answers 2

2

In push function, if you are passing char*, it will divert your pointer to where the char * is, and when you do p++, it will go from the char* you passed.

Try change push definition to:

void push(loods1 *loods, const char *name) {
  if (loods->size == STACK_MAX) {
    perror("Stack is full\n");
    exit(0);
  }
  else {
    strcpy((loods->data)[loods->size++], name);
  }
}

From here you might as well need some other changes to your calling program.

Also when you free it, free the single loods does not free all memory you allocated.

Sign up to request clarification or add additional context in comments.

2 Comments

You're totally right. It works now. Great, thanks! Another question: how should I free all the memory I allocated?
Also in C, be very careful when use = operator(which is what you might use most often). Pointer equation is always where the problem lies.
1

I flicked through the code and it seems to be ok, I think the problem is in your client.

Your only store pointers in the stack, you're probably pushing the same pointer on the stack but rewrite the string it points to.

Note that if you really only want to store pointers your 3rd malloc is wasting space and also creates a memory leak.

3 Comments

There will also be a memleak because the 2nd malloc is not freed either.
So should I free the mallocs as I edited in my original post?
I defined STACK_MAX, so why not?

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.