The size in bytes of the arguments to the ellipsis function

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 the buffer. I need to distribute the array dynamically, to which I can print the arguments (using sprinfor _vsprinf) For example, on a 32-bit OS, for myprintf ("% d% c", 10, "a"); the size of the arguments to myprintf is 5.

I tried to implement it like this:

va_list ap;

va_start(ap, fmt);

myArgSize(ap);

Can anyone advise how to implement myArgSize.

I was told to try something like this

char c;
int len = ::_vsnprintf(&c, 1, fmt, ap);

It does not work because more than one byte is written. But there is probably some workaround.

thank

+3
source share
4 answers

, , , , , . :

va_list ap;

va_start(ap, fmt);

int len = ::vsnprintf( NULL, 0, fmt, ap);
if (len < 0) abort();

char* buffer = (char*) malloc( len + 1);
if (!buffer) abort();

::vsnprintf( buffer, len+1, fmt, ap);

va_end();

, C99 vsnprintf() (, MSVC), . MSVC (, strlen(fmt)+1 + (10 * number_of_percent_signs_in_fmt) , 99% - , 200), , . - snprintf() Holger Weiss.

+1

... ; .

printf() -like , % - . . , %hd, short, int. , , , .

+1

vasprintf, , ( free).

, vsnprintf printf :

int n = vsnprintf(NULL, 0, fmt, args);

, , , . , .

+1

You can use sizeof (type_name) and navigate through all arguments, using for a loop, type_name - any of the data types of type c, such as int, float, long, etc. Then add all the bytes

-2
source

All Articles