• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3343
  • Last Modified:

create va_list from a array of char*

I am writting a JNI function for a c++ funtion that takes fn(a,b,c,...) variable arguments.  from JAVA, i pass a collection type and in native implementation i will get a native_fn(a,b,c, jobjectArray varlist);

from varlist, i will create array of char* as my data is array of strings only.

I want to create a va_list from a array of char*. ( array of strings), so that i will introduce a new function in c++ fn(a,b,c,va_list) and pass my dynamically created va_list.
have anyone done this before. how to create va_list from array of char* ?
  • 3
1 Solution
Why a va_list, why not pass an array ?

        fn(int a, int b, int c, char *varlist[], int len);
>> how to create va_list from array of char* ?

That's not what va_lists are used for btw ... They're just used for passing variable argument lists ... You know exactly what you'll pass (a few char*'s), so there's no reason to use va_list's.
To add to above information:

va_list is a very unsafe mechanism in C++ inherited from C. It can be misused to produce buffer overflows and to hijack a program if the programming was not made properly.

va_list has a few benefits as it allows to pass a variable number of arguments of arbitrary type to a function. But that benefit is exactly what makes it unsafe. In your case these benefits don't apply cause

  - you know what kind of arguments you need to pass
  - you can pass a variable number of these (string) arguments by passing the array

Thus, there is absolute no need to convert the array to a va_list which would be strange looking anyhow, e. g.

    char* szArgs[6] = { "abc", "xyz", .... , };
    function(szArgs[0], szArgs[1], szArgs[2], szArgs[3], szArgs[4], szArgs[5]);

Regards, Alex

ramalaksAuthor Commented:
I know why va_list is used and its purpose.  
In my native fn(abc,..) which takes variable arguments, inside that fn, they construct the va_list and pass that to a diff. fuction and that uses vsnprintf(). so if I consutruct a va_list, i can use the function without having to reimplenent the funtion in the C++ library.


i can not call my fn(a,b,c..) from my native implemnation as below
switch (no_of_varlist)
case 1:fn(&,Vars[0]); break;
case 2: fn(&,Vars[0, Vars[1]]); break;
case 3: fn(&,Vars[0], Vars[1],Vars[2]); break;
case 4: fn(&,Vars[0], Vars[1], Vars[2], Vars[3]); break;
....and go on till what "no"
if i have va_list from my array, i can call fn(a,b,c,va_list) , this way i dont have to reimplement the subseqeunt functions that fn() calls.
if i pass array of char* and no. fn(a,b,c,char *varlist[], int len) then i will have to reimplement fn and other fucntions that are called from fn.


I see what you mean.

The problem is that you have no (standard) way of initializing a va_list other than from a variable argument function's arguments.

Sometimes, the function that takes a va_list as argument will call another function that does the actual work. So, if you have access to the implementation of fn, check whether that's the case, and maybe you can call that other function directly (maybe even by passing an array to it).

If not, then I'm afraid you'll have to do something like the switch you showed, or implement your own fn function that takes an array as parameter.

If all array elements are treated the same way by the fn function, then maybe you can do something like :

        for (int i = 0; i < array_len; ++i) {
            fn("...", array[i]);

Check that. Maybe you can call snprintf directly :

        char str[STR_SIZE] = { 0 };
        int str_size = STR_SIZE;
        char *str_ptr = str;
        for (int i = 0; i < array_len; ++i) {
            int size = snprintf(str_ptr, str_size, "...", array[i]);
            size = (size >= str_size) ? str_size - 1 : size;
            str_ptr += size;
            str_size -= size;

It's not the prettiest code, but I hope you get what I mean ...

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now