Modifying a C++ array in main() from Lua without extra allocation

I am sketching a small C++ program that will pass arrays to Lua

The data will be float and the size will be really large,

My suggestion:

  • Keep the buffer on the C side (as a global variable for example)
  • Expose a C-function to LUA GetTableValue(Index)
  • Expose a C-function to Lua SetTableValue(Index, Value)

It should be something like this:

static int LUA_GetTableValue (lua_State *LuaState)
  float Value;

  /* lua_gettop returns the number of arguments */
  if ((lua_gettop(LuaState) == 1) && (lua_isinteger(LuaState, -1)))
    /* Get event string to execute (first parameter) */
    Offset = lua_tointeger(LuaState, -1);

    /* Get table value */
    Value  = LUA_FloatTable[Offset];

    /* Push result to the stack */

  /* return 1 value */
  return 1;

And you also need to register the function:

lua_register(LuaState, "GetTableValue", LUA_GetTableValue);

I let you write the SetTableValue but it should be very close. Doing so, the buffer is on C side and can be accessed from Lua with dedicated functions.