Lua C API: initializing a variable matrix in a C structure

I am trying to create userdata with the Lua C API with metatable related where I will collect the matrix.

What I cannot get is to set each component of the initialized matrix to zero.

I will compile the Lua code of the Cua module, as I described here .

The C code I have is as follows:

#include "lauxlib.h"
#include "lua.h"

typedef struct {
    LUA_NUMBER data[1][1];
    int row;
    int col;
}matrix;


// Create a matrix full of zeros
static int lb_newmatrix(lua_State *L)
{

    // Variable declarations
    int i,j;
    matrix *temp;

    // Input checks
    if (lua_gettop(L)!=2)
    {
       lua_pushstring(L,"\n Two input required");
       lua_error(L);
    }

    //--> Check I° index m riga
    luaL_checktype(L,1,LUA_TNUMBER);
    if (lua_tonumber(L,1)<0)
    {
        lua_pushstring(L,"\nRow number must be positive");
        lua_error(L);
    }

    //--> Check II° index n colonna
    luaL_checktype(L,2,LUA_TNUMBER);
    if (lua_tonumber(L,2)<0)
    {
        lua_pushstring(L,"\nColumn number must be positive");
        lua_error(L);
    }

    // Computation of memory allocation
    int m = lua_tonumber(L,1);
    int n = lua_tonumber(L,2);
    size_t nbyte = 2*sizeof(int)+sizeof(LUA_NUMBER)*m*n;
    size_t nbyte2 = sizeof(matrix)+sizeof(LUA_NUMBER)*(m*n-1);

    // Memory allocation
    temp = (matrix *)lua_newuserdata(L,nbyte);

    // Matrix dimension setting
    temp->row = m;
    temp->col = n;

    // Matrix inizialization
    /* PROBLEM HERE */
    for (i=1;i==m;i++)
    {
        for(j=1;j==n;j++)
        {
            temp->data[i][j] = 0;
        }
    }

    //-------------------------------
    // If I de-comment these line,
    // the matrix is written but 
    // element with equal sum indices
    // rewrite!!!
    //-------------------------------
    // temp->data[1][1] = nbyte;
    // temp->data[1][2] = nbyte2;
    // temp->data[1][3] = 13;
    // temp->data[2][1] = nbyte2;
    // temp->data[2][2] = 22;
    // temp->data[2][3] = 23; 
    // temp->data[3][1] = 31;
    // temp->data[3][2] = 32;
    // temp->data[3][3] = 33;

    // Link the userdata to the metatable "basic"
    luaL_getmetatable(L,"basic");
    lua_setmetatable(L,-2);

    return 1;
}

static int lb_index(lua_State *L)
{
    /* Check input Numbers */
    if (lua_gettop(L)>3)
    {
       lua_pushstring(L,"\nOnly two inputs are needed:\n1) Point\n2) N° row\n3) N° col");
       lua_error(L);
    }

    /* Check if the first input is userdata basic */
    matrix *temp = (matrix *)luaL_checkudata(L,1,"basic");

    /* I° index check ROW */
    luaL_checktype(L,2,LUA_TNUMBER); 
    if (lua_tointeger(L,2)<0||lua_tointeger(L,2)>temp->row)
    {
        lua_pushstring(L,"\n First index should be 1 to n");
        lua_error(L);
    }

    /* II° index check COLUMN */
    luaL_checktype(L,3,LUA_TNUMBER);
    if (lua_tointeger(L,3)<0||lua_tointeger(L,3)>temp->col)
    {
        lua_pushstring(L,"\n Second index should be 1 to m");
        lua_error(L);
    }

    int row = lua_tointeger(L,2);
    int col = lua_tointeger(L,3);

    /* Insert the index value of userdata on top of the stack */
    lua_pushnumber(L,temp->data[row][col]);

    return 1;
}


/**********************
 * MODULE DECLARATION *
 **********************/
static const struct luaL_Reg LuaBasic_f [] = {//
        {"NewMatrix",lb_newmatrix},
        {   "__index",  lb_index},
        {       NULL,        NULL}};

static const struct luaL_Reg LuaBasic_m [] = {//
        {        NULL,      NULL}};

LUA_API int luaopen_LuaBasic(lua_State *L)
{
    /* Insert basic metatable  "basic" into the stack */
    luaL_newmetatable(L,"basic");

    /* Copy the "basic" metatable
       and push it into the stack */
    lua_pushvalue(L,-1);

    /* basic["__index"] = basic */
    lua_setfield(L,-2,"__index");

    /* register all the function
       into LuaBasic_m into the
       basic table metatable */
    luaL_setfuncs(L,LuaBasic_m,0);

    luaL_newlib(L,LuaBasic_f);
    return 1;
}

The related Lua code is as follows:

lb = require("LuaBasic")
A = lb.NewMatrix(3,3)
print(A)
print("--------------")
print(lb.__index(A,1,1))
print(lb.__index(A,1,2))
print(lb.__index(A,1,3))
print(lb.__index(A,2,1))
print(lb.__index(A,2,2))
print(lb.__index(A,2,3))
print(lb.__index(A,3,1))
print(lb.__index(A,3,2))
print(lb.__index(A,3,3))
print("--------------")
print("row = "..lb.GetRow(A))
print("col = "..lb.GetCol(A))

and the output I get is:

userdata: 007C2940
--------------
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
1.#QNAN
--------------
row = 3
col = 3

I do not understand why I cannot write null values ​​to an initialized matrix.

What am I doing wrong?

+4
source share
2 answers

, ? . ... Lua, , . , ... ( )

lb_newmatrix ( return) lua_pushinteger( L, nbyte ); return 1; return 2;. ( , .) ,

static int lb_peek( lua_State *L ) {
    int nbytes = luaL_checkinteger( L, 2 );
    char *data = (char*)luaL_checkudata(L,1,"basic");
    lua_pushlstring( L, data, nbytes );
    return 1;
}

LuaBasic_m {"peek",lb_peek}. , Lua .

> m, size = lb.NewMatrix( 3, 3 )  -- make a small matrix
> data = m:peek( size )           -- get the memory as a string
> (("n "):rep(3*3).."I I"):unpack( data ) -- and unpack the values
0.0 6.366e-314 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0 0 81
-- (note: the trailing '81' is the offset of the next character in
-- the string beyond what was read and not part of your data)
-- so your matrix looks like:
-- 0.0 6.366e-314 0.0
-- 0.0    0.0     0.0
-- 0.0    0.0     0.0
-- and has 0 rows and 0 columns (according to the data…)

... , 6.366e-314 . , ...

> size/4
20
> (("I4"):rep(20)):unpack( data )
0 0 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 81

A-ha... ! (. 3, 3 ?)

, 1x1 LUA_NUMBER s... . , , , matrix *m, m->row *(m+sizeof(LUA_NUMBER[1][1])), , ...

, struct: !

 typedef struct {
     int row;
     int col;
     LUA_NUMBER data[1][1];
 } matrix;

, Lua :

> m, size = lb.NewMatrix( 3, 3 )
> data = m:peek( size )
> ("I I"..("n "):rep(3*3)):unpack( data )
3 3  0.0 0.0 0.0  0.0 0.0 0.0  0.0 0.0 0.0   81

3x3. . ,

/* m[i][j] = v */
static int lb_set( lua_State *L ) {
    matrix *m = (matrix*)luaL_checkudata(L,1,"basic");
    int i = luaL_checkinteger( L, 2 );
    int j = luaL_checkinteger( L, 3 );
    lua_Number v = luaL_checknumber( L, 4 );
    m->data[i][j] = v;
    return 0;
}

{"set",lb_set} LuaBasic_m. , :

> m, size = lb.NewMatrix( 3, 3 )
> for i = 0, 2 do for j = 0, 2 do
>>   m:set( i, j, (i+1) + (j+1)/10 )
>>   print( ("I I"..("n "):rep(3*3)):unpack( m:peek( size ) ) )
>> end end
3 3  1.1 0.0 0.0  0.0 0.0 0.0  0.0 0.0 0.0   81
3 3  1.1 1.2 0.0  0.0 0.0 0.0  0.0 0.0 0.0   81
3 3  1.1 1.2 1.3  0.0 0.0 0.0  0.0 0.0 0.0   81
3 3  1.1 2.1 1.3  0.0 0.0 0.0  0.0 0.0 0.0   81
3 3  1.1 2.1 2.2  0.0 0.0 0.0  0.0 0.0 0.0   81
3 3  1.1 2.1 2.2  2.3 0.0 0.0  0.0 0.0 0.0   81
3 3  1.1 2.1 3.1  2.3 0.0 0.0  0.0 0.0 0.0   81
3 3  1.1 2.1 3.1  3.2 0.0 0.0  0.0 0.0 0.0   81
3 3  1.1 2.1 3.1  3.2 3.3 0.0  0.0 0.0 0.0   81

... . - ?:-) data+(( 1 *i)+j)*sizeof(lua_Number), data+(( 3 *i)+j)*sizeof(lua_Number) ( ).

, , 1x1. ( , , i[m->data][j] m->data[i][j], .) , . , struct

 typedef struct {
     int row;
     int col;
     LUA_NUMBER data[0];
 } matrix;

([0] - . , data[1], struct. data[0] , struct - ( ), .)

m->data[i][j] m->data[m->col*i + j]. ( , , .)

:

> m, size = lb.NewMatrix( 3, 3 )
> for i = 0, 2 do for j = 0, 2 do
>>   m:set( i, j, (i+1) + (j+1)/10 )
>>   print( ("I I"..("n "):rep(3*3)):unpack( m:peek( size ) ) )
>> end end
3 3  1.1 0.0 0.0  0.0 0.0 0.0  0.0 0.0 0.0   81
3 3  1.1 1.2 0.0  0.0 0.0 0.0  0.0 0.0 0.0   81
3 3  1.1 1.2 1.3  0.0 0.0 0.0  0.0 0.0 0.0   81
3 3  1.1 1.2 1.3  2.1 0.0 0.0  0.0 0.0 0.0   81
3 3  1.1 1.2 1.3  2.1 2.2 0.0  0.0 0.0 0.0   81
3 3  1.1 1.2 1.3  2.1 2.2 2.3  0.0 0.0 0.0   81
3 3  1.1 1.2 1.3  2.1 2.2 2.3  3.1 0.0 0.0   81
3 3  1.1 1.2 1.3  2.1 2.2 2.3  3.1 3.2 0.0   81
3 3  1.1 1.2 1.3  2.1 2.2 2.3  3.1 3.2 3.3   81

, .

, [1], [2], [3] [0], [1], [2]. C . [0] not [1], [size-1] [size]. (n + 1) x (m + 1) 1... n 1... m, ( 3x3, , ). , , C.

Lua, . (data[3][3] 3x3, "" - data[2][2] data[m-1][n-1].) , Lua/index (1-based, 1 n) C/offset (0-based, offset 0 (n-1)). ( , / 0, , , , .)

+5

Lua; C. , temp->data[i][j], temp->data , , C , temp->data 1x1.

,

typedef struct {
    int row;
    int col;
    LUA_NUMBER data[1];
}matrix;

, data .

i, j data[i*col + j].

+4

Source: https://habr.com/ru/post/1678049/


All Articles