[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Variable length arguments in C/C++
- From: Sean Conner <sean@...>
- Date: Mon, 8 Aug 2022 15:07:34 -0400
It was thus said that the Great Jairo A. del Rio once stated:
> Hi, list. I want to ask you about this:
> Concretely, I want to know if nowadays (2022) there's a simpler or safer
> way to pass variable length arguments from Lua to C/C++.
Short answer: No.
Long answer: No, but ...
> My question
> concerns libraries using variadic functions, so setting a fixed number of
> arguments is not an option to me. Thank you in advance.
What type of function are you trying to call? If it's in the printf()
style family (format string, extra parameters), then, as stated on the Stack
Overflow page you linked, calling string.format() is your best bet. You can
even do this at the C level:
static int foo(lua_State *L)
lua_call(L,lua_gettop(L) - 1,1);
If it's some other type of function, say execlp()  then finding an
alternative that doesn't use variable arguments, like execve() (which uses
arrays) is your best bet.
Now, it's not impossible to do what you are asking for, but it can't be
done in C. No, you'll need to drop down to assembly language  if you
really want to go this route. Assembly will give you the control needed to
set up the stack (and/or registers) needed to make the call. But this
approach is, by its very nature, very CPU and OS dependent.
 From a link on the State Overflow page:
 Yes, I have written a Lua module in assembly. It's not hard (if you
know assembly), just tedious.