Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

After reading the book from which this example is from I started using APIs like this

typedef struct stack {
    char **items;
    int len;
} stack;

stack *stack_new() {
    stack *out = malloc(sizeof(stack));
    //initialize  the struct
    *out = (stack) {};
    return out;
}

void add(stack *in, char *val) {
    in->len++;
    in->items = realloc(in->items, in->length * strlen(val));
    in->items[in->length-1] = val; //<- actually this would be better with strcpy, but you get the idea
}

problem is that valgrind hates it. I've been trying to fix the various memory leaks that structs similar to this one (and to the dictionary of the link) are creating, until I started wondering if it's a good pattern at all.

share|improve this question
    
What is stuff? – 200_success May 26 at 17:57
    
it was a typo, I fixed it. – fra9001 May 27 at 7:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.