当前位置: 动力学知识库 > 问答 > 编程问答 >

Print address of string in 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?

网友答案:

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?

分享给朋友:
您可能感兴趣的文章:
随机阅读: