Wrox Programmer Forums Confusion about what exactly is created in memory
 |
BOOK Beginning Lua Programming ISBN: 978-0-470-06917-2
 This is the forum to discuss the Wrox book Beginning Lua Programming by Kurt Jung, Aaron Brown; ISBN: 9780470069172 Read more about Beginning Lua Programming or buy the book from your favorite retailer Download the code for Beginning Lua Programming
 Welcome to the p2p.wrox.com Forums. You are currently viewing the BOOK Beginning Lua Programming ISBN: 978-0-470-06917-2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com

June 2nd, 2007, 10:07 AM
 SingyBob Registered User Join Date: Jun 2007 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts
Confusion about what exactly is created in memory

Hi! I am unclear about a few things which I wondered if you could shed light upon:
1) In the MakeRing(Ring) function of pg 145. When the functions are being assigned to the Ring table:
for Name, Fnc in pairs(Methods) do
Ring[Name] = Fnc
end
are complete copies of the functions being stored in the ring table, or simply pointers to the already existing functions in the Methods table?

2) In a similar vein, when functions are created as closures, is it a copy of the upvalue referred to in the external function that is created in the function, or simply a pointer to the upvalue?
E.g. using the example from the previous post:
local A = 10

local function Fnc(B)
return A + B
end

print(Fnc(5))

when Fnc is created is a copy of the value of A (10) stored in Fnc or simply a pointer to A itself?

Regards, Ken

June 2nd, 2007, 11:47 AM
 Kurt Jung Wrox Author Join Date: Apr 2006 Posts: 9 Thanks: 0 Thanked 0 Times in 0 Posts

Hi Ken,

Good questions!

> When the functions are being assigned to the Ring table:

> for Name, Fnc in pairs(Methods) do
> Ring[Name] = Fnc
> end

> are complete copies of the functions being stored in the ring
> table, or simply pointers to the already existing functions
> in the Methods table?

The pointer model is closer to the truth (the assignment is
"cheap", not "deep"), although see Rici Lake's excellent
posting to the Lua mailing list for a clearer and more accurate
way of looking at references in Lua:

http://lua-users.org/lists/lua-l/2005-09/msg00056.html

> In a similar vein, when functions are created as closures, is
> it a copy of the upvalue referred to in the external function
> that is created in the function, or simply a pointer to the
> upvalue?

Again, the pointer model is more (but not completely) accurate.
By adding a couple lines to your example, you can see that it
is A, not the value A happens to be mapped to, that is captured
by Fnc as an upvalue.

local A = 10

local function Fnc(B)
return A + B
end

print(Fnc(5)) --> 15

A = 20

print(Fnc(5)) --> 25

All the best,

Kurt

June 2nd, 2007, 12:40 PM
 SingyBob Registered User Join Date: Jun 2007 Posts: 2 Thanks: 0 Thanked 0 Times in 0 Posts

Thanks for the speedy reply Kurt. I shall read Rici's article with interest. Regards, Ken.

 Similar Threads Thread Thread Starter Forum Replies Last Post Confusion with PLATFORM, Help me Please! dpkbahuguna .NET Framework 2.0 10 September 28th, 2007 08:41 AM foreach confusion stokerfed C# 2 June 13th, 2007 02:30 PM Class' Confusion dpkbahuguna Beginning VB 6 4 September 20th, 2006 11:16 PM Some confusion as to which SQL to use mark.uk SQL Server 2000 6 August 12th, 2004 10:47 AM Stream Confusion benburrows General .NET 1 February 27th, 2004 12:04 PM