vsprintf and va_list - string length?
Ok, so how to explain this easy?
Well I have a function that takes a char* and a va_list as arguments. It will then send the complete, processed string to another function. Example: Code:
void function(char *txt, va_list args) 1) Use char* with malloc. I need to know how long the complete string will be to avoid having to chop it. 2) Use string. But can I use it with a va_list? |
Hi
I don't think there is a standard C/C++ way of handling this. But GNU has some functions that are not standard, but avaiable in Linux: vasprintf and asprintf. They work like sprintf, but you don't pass a string pointer, but the address of one. The string is then allocated for you, so you don't need to worry about size. Example: Code:
#define GNU_SOURCE |
Does the standard vsnprintf with a small size argument (1 for example) return the size minus one required by your malloc? If so it could avoid you to use nonstandard functions, but it could reduce a lot the speed of your code too because of a double call to vsnprintf.
|
All times are GMT -5. The time now is 07:02 PM. |