Skip to content

Instantly share code, notes, and snippets.

@foobit
Last active December 11, 2015 14:59
Show Gist options
  • Save foobit/4618064 to your computer and use it in GitHub Desktop.
Save foobit/4618064 to your computer and use it in GitHub Desktop.
64bit (OSX and *nix) vsnprintf trashes va_list args. Must va_start between requesting length and performing expansion
std::string string_format(const char* s, ...)
{
va_list args;
va_start(args, s);
std::string result;
int len = vsnprintf(nullptr, 0, s, args);
if (len > 0)
{
// va_end/va_start hack to fix *nix 64bit versions inc Mac OS X
va_end(args);
va_start(args, s);
scope_array<char> tmp(len+1);
vsnprintf(tmp.get(), tmp.size(), s, args);
result = tmp;
}
va_end(args);
return result;
}
@foobit
Copy link
Author

foobit commented Jan 24, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment