Skip to content

Instantly share code, notes, and snippets.

@Cactus64k
Created January 24, 2017 23:02
Show Gist options
  • Save Cactus64k/0d3f9befd76e62fcde66e5bb3f97b7d2 to your computer and use it in GitHub Desktop.
Save Cactus64k/0d3f9befd76e62fcde66e5bb3f97b7d2 to your computer and use it in GitHub Desktop.
int alloc_snprintf(char** out, const char* template, ...)
{
va_list list;
va_start(list, template);
int len = vsnprintf(NULL, 0, template, list);
va_end(list);
char* new_string = malloc((size_t)len + 1);
assert(new_string != NULL);
*out = new_string;
va_start(list, template);
len = vsnprintf(new_string, (size_t)len+1, template, list);
va_end(list);
return len;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment