I'm experimenting with writing a dynamic string library in C, and I decided to write an implementation of sprintf.In my code, I call vsnprintf with a buffer of 0 bytes, just so I can get the number of bytes it requires. I was wondering if there is anything wrong with this code and if there a better way to do this. It feels wrong to me, but I can't seem to think of a better way to do so yet have the same effect.string string_printf(const char *fmt, ...) {    va_list in, copy;    string a = NULL;    int size;    /* start the main input */    va_start(in, fmt);    /* make a copy of the input */    va_copy(copy, in);    /* run vsnprintf just to see how much bytes needed */    size = vsnprintf(a, 0, fmt, copy) + 1;    va_end(copy);    /* allocate the amount now */    a = string_new_size(size);    string_to_sr(a)->len = size - 1;    /* re-run */    vsnprintf(a, size, fmt, in);    va_end(in);    /* done */    return a;}
Implementing printf to a string by calling vsnprintf twice
c;memory management;formatting;library;variadic