🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Lua : Pointers / Reference Variables

Started by
14 comments, last by Ironica 20 years, 11 months ago
quote: Original post by Sneftel
I agree that the manual is confusing. One factor in this is how strings in Lua are stored; there's a global hash-table. So if you do

a = "foo"
b = "foo"

Then a and b actually point to the same location. The terminology is thus confusing, but the behavior is not; just treat every variable as a reference, and every datum as an object.

Ohh, this is interesting, hehe.
map1 = createmap()map2 = createmap()  

So are you saying map1 and map2 point to the same location? My function (createmap()) creates a table, which holds the map's data, and returns it, by the way. Or were you just talking about strings? Well how about if I had something like this:
function createmap(idx) return {  id = idx  -- (The rest of the table here) }endmap1 = createmap(1)map2 = createmap(2)  

My tables wouldn't be identical, so would be 2 seperate tables, right?
Thanks for that guys =)

[edited by - Ironica on July 22, 2003 10:16:34 PM]
Advertisement
quote: Original post by Ironica
Or were you just talking about strings?
Whoops, yeah, should''ve made that clear. I''m just talking about strings.

JuNC, I agree with you about the dangers of function mutability. However, since this mutability is so limited, there''s relatively little danger of unexpected results; you have to go moderately far out of your way to get truly mutable functions.

How appropriate. You fight like a cow.
Hehe good, that makes things easier =)

So if I called that createmap() function twice, it would return 2 identical tables, not 2 references to 1 table? But if I assigned them to the same value, they would be references to the same table?
map1 = createmap() -- 1 tablemap2 = createmap() -- 2 tables?map1 = createmap() -- 1 tablemap2 = map1        -- same table? 
Well, we don't know what createmap() actually does so it's hard to answer
EDIT: ignore the above line, I missed your post above.

Basically, each table you create using a table constructor, e.g.

table1 = { x = 2, 4, 5 }

Exists only once in memory. It is never copied when you perform an assignment.

So, if createmap() looks like this:

function createmap()
local table = { x = 4, y = 2 }
table.x = 4
return table
end

Then

map1 = createmap() -- a new table
map2 = createmap() -- a different table
map1 = createmap() -- a third table. The table created in the first createmap() is now not referred to and will be garbage collected
map1 = map2

map1 now refers to the table that the second createmap() created. The table created by the third createmap() call will be marked for garbage collection as there are no references to it (or similar, I don't know much about the Lua GC)



[edited by - JuNC on July 23, 2003 5:02:29 AM]
also keep in mind that two identical tables, by default, will not be seen by Lua as equal to each other. If you want different behavior, you must define an equality metamethod for them.

How appropriate. You fight like a cow.
Excellent, thanks for the help guys =) I''m learning, hehe.

This topic is closed to new replies.

Advertisement