I have a function :
static int myprintf(const char* fmt, ...)
I want to know the size in bytes of all myprintf arguments,if they were printed to buffer.
I need allocate an array dynamically to which I can print the the arguments (using sprinf or _vsprinf)
For example in 32bit OS,for myprintf(“%d %c”,10,’a’);
the size of myprintf arguments is 5.
I tried to implement it like:
va_list ap;
va_start(ap, fmt);
myArgSize(ap);
Can someone advise how to implement myArgSize.
I was told to try something like this
char c;
int len = ::_vsnprintf(&c, 1, fmt, ap);
It doesn`t work since more than one byte is written.
But probaly there is some workaround.
Thanks
Even though much of your question is about the size of the arguments passed to your triadic function, it seems to me that you’re actually interested in how big the output buffer needs to be. Try something like the following:
However, if your platform doesn’t have a C99 standard compliant
vsnprintf()(for example, MSVC) then you’ll have to make some adjustments. For MSVC You could try passing in a buffer sized with a guess (maybestrlen(fmt)+1 + (10 * number_of_percent_signs_in_fmt)or just start with a size that should cover 99% of your cases – maybe200), and increase the guess until it works. Or you might try something like thesnprintf()family of functions by Holger Weiss.