Faster way of converting a 1-d table to predefined userdata in Lua
You have a duplication of the allocation and init part in the 2 functions, instead you can create a helper that will create the usertable, return a pointer to it and leave it on top of the stack:

static FloatArray *create_array(lua_State *L, int n) {
  size_t nbytes = sizeof(FloatArray) + (n - 1) * sizeof(float);
  FloatArray *a = (FloatArray *)lua_newuserdata(L, nbytes);
  a-&gt;size = n;
  return a;
}

static int new_array(lua_State *L) {
  int n = luaL_checkint(L, 1);
  create_array(L, n);
  return 1;
}


When accessing parameters in the C api it's easier to always index them from the bottom of the stack. That wat you won't end up being mistaken about the state of the stack and try to use the wrong value by accident (like you did in your attempt).

static int new_array_from_table(lua_State *L) {
  luaL_argcheck(L, lua_type(L, 1) == LUA_TTABLE, 1, "'1-d array' expected");

  // get the length of input table
  int n = lua_objlen(L, 1);

  // create new userdata
  FloatArray *a = create_array(L, n); 
  //leaves the new table at the top of the stack

  // fetch data and use it to fill the array
  int index;
  for (index = 1; index &lt;= n; index++) {
    lua_rawgeti(L, 1, index);
    a-&gt;values[index-1] = (float)lua_tonumber(L, -1);
    lua_pop(L, 1);
  }
  return 1;
}


With the creation bit in a single place you can now add a metatable to it so the script can access the values using the indexing syntax.

luaL_Reg[] floatArrayFuncs = { {"__index", get_array_value},
         {"__newindex", set_array_value},
         //...
         {NULL, NULL},};

//call on initialization
static void init_FloatArray(lua_State *L){
    luaL_newmetatable (L, "FloatArray");
    luaL_setfuncs(L, floatArrayFuncs , 0);
    lua.pop(1);
}

static FloatArray *create_array(lua_State *L, int n) {
  size_t nbytes = sizeof(FloatArray) + (n - 1) * sizeof(float);
  FloatArray *a = (FloatArray *)lua_newuserdata(L, nbytes);
  luaL_setmetatable (L, "FloatArray", -1); 
  a-&gt;size = n;
  return a;
}


The metatable also allows you an easy way to type check the userdata from lua using 

FloatArray *a = (FloatArray *)luaL_testudata (L, arg, "FloatArray");


Also you can overload the creation function by doing

static int new_array(lua_State *L) {
  switch(lua_type(L, 1)){
  case LUA_TNUMBER:
      int n = luaL_checkint(L, 1);
      create_array(L, n);
      return 1;
  case LUA_TTABLE:
      return new_array_from_table(L);
  case LUA_TUSERDATA:
      return duplicate_array(L);  //exercise for the reader
  default:
      luaL_argcheck(L,0, 1, "expected number, 1D array or FloatArray");
  }
  return 0; //never reached
}