Instead of allocating on the stack first and then allocating on the heap afterwards, you should just allocate with malloc() to begin with (since you already have the stringlen). Just malloc() directly to `state->outputs[current_output].output` and do a single strcpy.