I want to use mysprintf () instead of sprintf () for automatic buffer size allocation.
Is there any problem with mysprintf()? Or can you recommend a better way?
char s[256];
sprintf(s, "%s-%s-%s", "abcdefg", "abcdefg", "abcdefg");
string s = mysprintf("%s-%s-%s", "abcdefg", "abcdefg", "abcdefg");
string mysprintf(const char* format, ...)
{
int ret;
char* buf;
va_list ap;
va_start(ap, format);
ret = vasprintf(&buf, format, ap);
va_end(ap);
if (ret == -1) {
return {};
}
string out(buf);
free(buf);
return out;
}