Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pointers Lua表格->;地址和地址->;桌子_Pointers_Lua_Lua Table - Fatal编程技术网

Pointers Lua表格->;地址和地址->;桌子

Pointers Lua表格->;地址和地址->;桌子,pointers,lua,lua-table,Pointers,Lua,Lua Table,想象一下下面的代码: Mytable={} print(Mytable) 打印类似于表:12345的内容。 如何从Lua获取“地址”部分而不影响到字符串的返回值,更重要的是,如何获取表 代码: addr=table2address(Mytable) -- type(addr) is number, addr is 12345 Othertable=address2table(addr) -- type(Othertable) is table, Othertable==Mytable is t

想象一下下面的代码:

Mytable={}
print(Mytable)
打印类似于
表:12345
的内容。 如何从Lua获取“地址”部分而不影响
到字符串的返回值,更重要的是,如何获取表

代码:

addr=table2address(Mytable)
-- type(addr) is number, addr is 12345
Othertable=address2table(addr)
-- type(Othertable) is table, Othertable==Mytable is true (same reference)
有没有办法在Lua中实现这两个功能?如果没有,(如何)在C中执行此操作


编辑:
table2address
可以通过将
Table:
tostring(Mytable)
中删除来完成,但只有在metamethod
\uu tostring
未定义的情况下,我才想避免这种情况。

一个简单的实现满足您的所有标准,只有一个:

function table2address(Mytable) return Mytable end
function address2table(addr) return addr end
演示:

稍微复杂一点的实现符合您的所有标准:

t2at = {}

function table2address(Mytable) 
  local addr = t2at[Mytable]
  if addr == nil then
    addr = #t2at + 1
    t2at[Mytable] = addr
    t2at[addr] = Mytable
  end
  return addr
end

function address2table(addr)
  return t2at[addr]
end
演示:

那么,为什么地址对你很重要

在Lua这样的垃圾收集语言中,只能保存对对象的引用,而不能保存地址。[目前的实现可能在GC期间移动对象,也可能不移动对象,但除了
userdata
和Lua声明之外,Lua拥有移动任何东西的许可证。]

附录

Re:“地址永远不会随机化(在两个新的交互式lua实例中尝试打印({})”

Re:确实需要物理地址

看看函数
luaL_tolstring
,它实现了打印的精髓;它有(在Lua 5.2.2中):


因此,
lua_topointer(L,idx)
是获取表地址所需的函数。

为什么需要此函数?@nonchip正如@DougCurrie的回答中所示,表(或除
nil
之外的任何值)可以用作表键。这几乎满足了你所要求的一切需求。你有特例吗?+1做得好!在避免实现细节的同时满足规定的标准。很抱歉,这两种方法都不符合标准:第一种方法是一个糟糕的笑话,只是返回输入。第二个实现了一个数字自动增长查找表。回答你的问题:这个地址对我来说很重要,因为很明显可以从lua(参见tostring)获得它,所以它存在并可以用来引用这个表。由于地址永远不会随机化(在两个新的交互式lua实例中尝试
print({})
),因此它们可以单独用于处理相同的表进程。我知道这是不可靠的,但当我试图实现一些类似于冻结代码的函数作为一个techdemo时,我们鼓励用户尝试使用这些值。换句话说:我知道有很多列表/数组/任何实现,所以地址真的是唯一重要的东西,因为我不想要任何列表/可查找/。。。但只是一个窥视/戳的等效功能。@nonchip,请参阅我的附录,其中说明了您的意见。
t2at = {}

function table2address(Mytable) 
  local addr = t2at[Mytable]
  if addr == nil then
    addr = #t2at + 1
    t2at[Mytable] = addr
    t2at[addr] = Mytable
  end
  return addr
end

function address2table(addr)
  return t2at[addr]
end
> Mytable={}
> addr = table2address(Mytable)
> Othertable=address2table(addr)
> =type(Othertable)
table
> print(Othertable==Mytable)
true
> =type(addr)
number
e$ lua
Lua 5.2.2  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> print({})
table: 0x7fdaca4098c0
> ^D
e$ lua
Lua 5.2.2  Copyright (C) 1994-2013 Lua.org, PUC-Rio
> print({})
table: 0x7fb02a4098c0
> ^D
e$ 
  default:
    lua_pushfstring(L, "%s: %p", luaL_typename(L, idx),
                                        lua_topointer(L, idx));
    break;