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.
stuff
? – 200_success♦ May 26 at 17:57