Determining size of vsprintf output

1036 views c++
8

I am implementing a printf(const char * format, ...) like method for a class, where I need to determine the exact size of it's output given only the supplied format and the arguments given in va_list, before calling vsprintf() to perform the actual writing.

Is there a function that can take the format and va_list to generate the exact length of the output?

answered question

va_list is extremely error prone in that it will let users pass an elephant when an integer is expected and not indicate any failure, but instead enter the land of UDB, usually with very bad outcome. It completely relies on the format specifier for the type and size assumptions and does the equivalent of a reinterpret_cast. If you are in a C++ shop, I'd highly recommend using a more type safe means to your ends. These C-isms exist for backwards compatibility. Don't pollute your shiny new code with void pointer abuse and UDB

1 Answer

Have an answer?

JD

Please login first before posting an answer.