Jump to content
  • Advertisement
Sign in to follow this  

[C++] Having problems with variable arguments function

This topic is 3815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a function with variable arguments that calls a Lua function. However, at the Lua end the arguments are wrong - it seems they are in the wrong order. If I do:
CallFunction("FunctionName", "ssd", "Hello", "There", 12.3);
Lua sees it as:
FunctionName("Hello", "FunctionName", Garbage float)
Here is the function:
void SYM_SCRIPT::CallFunction(string Function, const char *ArgSignature, ...)
	    //Check Lua function exists
	    lua_pushstring(LuaState, Function.c_str());
        lua_rawget(LuaState, LUA_GLOBALSINDEX);
        bool Exists = lua_isfunction(LuaState, -1);
        lua_pop(LuaState, 1);

            //Number of arguments
            int NumArgs = 0;

            va_list VAList;
            va_start(VAList, ArgSignature);

            //Get function
            lua_getglobal(LuaState, Function.c_str());

            //Push arguments
            while (*ArgSignature)
                switch (*ArgSignature++)
                    case 'd':
                        //Double argument
                        lua_pushnumber(LuaState, va_arg(VAList, double));

                    case 'i':
                        //Int argument
                        lua_pushnumber(LuaState, va_arg(VAList, int));

                    case 's':
                        //String argument
                        lua_pushstring(LuaState, va_arg(VAList, char *));

                    default: break;


            //Do the call
            int Error = lua_pcall(LuaState, NumArgs, LUA_MULTRET, 0);

            //Check for errors
            LuaError(LuaState, Error);


I'm afraid I don't have the original article that showed how to write the function, but it should be pretty self explanatory. At this stage I'm trying to narrow it down to whether it's a C++ va_arg problem or a Lua problem.

Share this post

Link to post
Share on other sites
Original post by RDragon1

luabind is my next adventure, but the above is educational and I'd love to get it working.

Share this post

Link to post
Share on other sites
Step through the function with the debugger to determine whether the arguments are being processed in the correct order.

Share this post

Link to post
Share on other sites
I've stepped through, and the order in which arguments are retrieved doesn't make sense.

- The first returned argument is, as it should be, the first variable arg.
- The second returned argument is the FIRST argument of the function (string Function).
- Everything after seems to be garbage.

After a lot of Googling, I've found a few gotchas with the va_ functions.

MSDN confuses me, by mentioning a UNIX alternative method:


/* Returns the average of a variable list of integers. */
#ifdef ANSI /* ANSI compatible version */
int average( int first, ... )


#else /* UNIX compatible version must use old-style definition. */
int average( va_alist )


linux.about.com contradicts this, and talks about the usual va_ functions, not noting any differences between a UNIX implementation and any other platform:


void va_start(va_list ap, last);
type va_arg(va_list ap, type);
void va_end(va_list ap);
void va_copy(va_list dest, va_list src);

cppreference.com describes the second parameter of va_start as the "mandatory first argument of the function":

First, you must have a call to va_start() passing a valid va_list and the mandatory first argument of the function.
This first argument can be anything; one way to use it is to have it be an integer describing the number of parameters being passed.

whereas other references say it should be the "name of the last parameter before the variable argument list".

What is the correct way to use this set of functions? Portability is priority, I'd rather not have #ifdef's all over the place.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!