Two variables pointing to same mem location?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
TimeLoop
Prole
Posts: 28
Joined: Wed Apr 17, 2013 8:20 am

Two variables pointing to same mem location?

Post by TimeLoop »

Hi.

Lets say we got a piece of code like the next one:

Code: Select all

foo = {salute = "Hello World!"} -- some LUA table

myTable = nil
original = nil

function loadTable(tableName)
    myTable = tableName
end

function restoreOriginal()
    loadTable(original)
end

function love.load()
    original = foo
    loadTable(foo)
    myTable.salute = myTable.salute .. " Goodbye World!"
    restoreOriginal()
    print(myTable.salute) --====>> OUTPUTS "Hello World! Goodbye World!"
end



Anyone knows the problem there?

I noticed also that when I do "print(foo.salute)", the foo table got modified!!! How can that be possible?

Thanks
User avatar
veethree
Inner party member
Posts: 875
Joined: Sat Dec 10, 2011 7:18 pm

Re: Two variables pointing to same mem location?

Post by veethree »

You're setting mytable and original to nil which is nothing basically. To define an empty table you would do this:

Code: Select all

mytable = {}
original = {}
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Two variables pointing to same mem location?

Post by Azhukar »

Tables are references, the only time a new table is created is when you use {}.

mytable = {}
thisIsAReference = mytable

The above makes both variables point to the same exact table.
ixjf
Prole
Posts: 19
Joined: Sat Apr 27, 2013 10:27 am

Re: Two variables pointing to same mem location?

Post by ixjf »

As aforementioned, foo variable is a reference to the table, it doesn't hold the actual table. It is possible, though, to do what you want by creating a copy of the original table, although you have to copy field by field and save metatables yourself (I suppose there are some implementions on the internet, perhaps search for table.deepcopy or deep_copy).

EDIT: Close enough! It was deepcopy :P

Code: Select all

function deepcopy(orig)
    local orig_type = type(orig)
    local copy
    if orig_type == 'table' then
        copy = {}
        for orig_key, orig_value in next, orig, nil do
            copy[deepcopy(orig_key)] = deepcopy(orig_value)
        end
        setmetatable(copy, deepcopy(getmetatable(orig)))
    else -- number, string, boolean, etc
        copy = orig
    end
    return copy
end
Source: http://lua-users.org/wiki/CopyTable
I used to be an adventurer like you, but then I took an arrow in the knee.
User avatar
TimeLoop
Prole
Posts: 28
Joined: Wed Apr 17, 2013 8:20 am

Re: Two variables pointing to same mem location?

Post by TimeLoop »

Copying tables still doesn't work.


The values of the original table get modified anyway.


Any other solution?
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: Two variables pointing to same mem location?

Post by Azhukar »

TimeLoop wrote:Any other solution?
Yes, learn basic lua.

http://lua-users.org/wiki/TablesTutorial
ixjf
Prole
Posts: 19
Joined: Sat Apr 27, 2013 10:27 am

Re: Two variables pointing to same mem location?

Post by ixjf »

TimeLoop wrote:Copying tables still doesn't work.
The values of the original table get modified anyway.
Any other solution?
What are you talking about? How do they get modified with a copy of the original table? Assigning a table variable to another variable doesn't copy it.
I used to be an adventurer like you, but then I took an arrow in the knee.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 0 guests