-1

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; } 
5
  • 1
    You are walking a path of great pain, trying to take all of the disadvantages of sprintf with little to show for it. Has anyone introduced you to parameter packs? This will still be painful as all hell, but type safe. Commented Sep 4, 2019 at 4:52
  • Seems fine to me in principle (ignoring type safety), although strictly speaking it's not exception-safe; if std::string's constructor throws, you've leaked memory. Commented Sep 4, 2019 at 5:25
  • Unless you want to learn about what's wrong with your existing code. Commented Sep 4, 2019 at 5:26
  • At the very least, you should wrap the allocated buf in a std::unique_ptr with a custom deleter to call free() Commented Sep 4, 2019 at 6:12
  • vasprintf() is GNU specific. If your compiler is compatible with C99 - or supports the C99 standard library - you can use vsnprintf() to, (1) obtain a required buffer (2) allocate a container (std::string or a std::vector<char>) of appropriate size and (3) write the data directly to that container. The benefit of this alternative is that all the allocations are cleanly deallocated (no need to call free() manually, and no opportunity to inadvertently not do so). Commented Sep 4, 2019 at 6:28

1 Answer 1

-1

You don't need to write your own function, use asprintf() instead.

But notice that it is a USE_GNU stuff.

Remember to free()

Sign up to request clarification or add additional context in comments.

2 Comments

The point is to make a C++ wrapper around asprintf so that memory is managed via RAII.
Yes, @ jamedin's comment is my point.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.