dimanche 16 juin 2019

Variadic char array shows up incorrectly in nested function C++

I have two functions for a windows wrapper class, through which Im trying to pass a variadic argument list of char* arrays

The first one is:

bool OsInterface::AddDropdown(std::string menu_item_name, RECT v_location, int num_entries, ...)
{
    bool ret_val = true;
    va_list v_args;
    va_start(v_args, num_entries);
    //char* item_name = va_arg(v_args, char*);
    if (!windowCreator.AddDropdown(menu_item_name, v_location, num_entries, v_args))
        ret_val = false;
    va_end(v_args);
    return ret_val;
}

The second one is

bool WindowCreator::AddDropdown(std::string drop_down_name, RECT v_location, int num_entries, ...)
{
    va_list v_args;
    va_start(v_args, num_entries);
    std::vector<std::string> item_names;
    for(int i = 0; i < num_entries; i++)
    {
        char* item_name = va_arg(v_args, char*);
        item_names.push_back(item_name);
    }
    va_end(v_args);
    //Unrelated code following    
}

and I'm calling it this way:

if (!osInterface.AddDropdown("Menu_Dropdown", v_position, 1, "Item1"))

Here is the problem:

When I uncomment the line in the first function: char* item_name = va_arg(v_args, char*);

I can see that item_name is in fact "Item1"

However, when I do the same thing in the second function, it item_name points to a completely different memory location and I get some garbage like this: "\Ú±"

Note that I still have the same line commented in the first function when I do this.

How do I correctly pass a variadic argument char array or string to a nested function?

Edit: I understand that these are not technically nested functions because function 2 is not defined within function 1, I couldn't find a better term to use for it

Thanks,

Aucun commentaire:

Enregistrer un commentaire