Lua中打印字符串的地址
我想在Lua中打印字符串的起始地址。那么有什么方法可以让我做到这一点吗?例如:-
x = "abc"
现在我想打印存储 abc 的地址。那么有什么办法可以做到吗?然后我会将这个 x 传递给 C 程序,然后尝试打印收到的 x 的地址,以查看 lua 不会复制字符串,而是使用对基地址的引用。
那么有什么方法可以做到这一点吗?
I want to print the starting address of string in Lua. So is there any way using which I can do that? for ex:-
x = "abc"
Now I want to print the address where abc is stored. So is there any way to do it? I will then pass this x to a C program and then try to print the address of x received to see that lua doesn't copy string but uses reference to the base address.
So is there any method to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Lua 中没有办法获取字符串的地址。
可能是因为没有必要。无论如何,字符串都是内化的。所以每个字符串在内存中只存在一次。将字符串文字分配给变量会检查用于存储字符串的哈希表。如果未找到,则添加字符串,并且变量获取对已加载字符串的引用。如果找到,它只会获取对现有字符串的引用。 Lua 5.0的实现细节,字符串处理在5.1 IRC中没有太大变化。
这直接回答了您需要它的问题,不是吗?
There is no way to get the address of a string in Lua.
Probably because there is no need to. Strings are internalized anyhow. So every one string is in memory exactly once. Assigning a string literal to a variable checks the hashtable used to store strings for the string. If not found the string is added, and the variable gets the reference to the loaded string. If found, it just gets a reference to the existing string. Implementation details of Lua 5.0, the string handling hasn't changed hugely in 5.1 IRC.
That directly answers the question you needed it for, no?