lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

From: Leo Razoumov
> All Lua variables are, indeed, *references* to Lua objects.
> str= "Hello"
> local s= str
> Both 's' and 'str' refer to the very same object, string "Hello".

As a scripter (and not script integrator) the above doesn't matter to me
much. Whether immutable objects (strings, booleans, numbers) use a
shared reference or unique instances, the end result is identical to me
as a scripter. But I'm curious as to the inner workings.

Is the above quote really true? The Lua 5.1 manual says (excerpted):

   "There are eight basic types in Lua: nil, boolean, number, string,
    function, userdata, thread, and table.
    Tables, functions, threads, and (full) userdata values are objects:
    variables do not actually contain these values, only references to

My layman interpretation of the second sentence above was that variables
thus *do* 'contain' values that are not object types. (I realize that
this is technically a logical fallacy, but a reasonable interpretation
of the intent.) Thus, I had thought that the code...
   local a = "foo"
   local b = "foo"
   local c = b
...meant that 3 strings were allocated in memory internally, one to be

Are 3 strings allocated and maintained internally? (Each assignment of a
non-object value copies it 'into' the variable.)

Or is it 2? (One for each string literal, with the latter shared by 'b'
and 'c'.)

Or is it 1? (Are Lua's internals tricky enough that they 'hash' a string
and only allocate memory for each unique sequence?)