lua-users home
lua-l archive

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


在 2016/8/5 19:22, Niccolo Medici 写道:
Let's say I have a C 'int' variable that holds a reference (what
luaL_ref() returns). At the start of the program it's initialized to
zero. Can I assume that zero means that it doesn't hold a reference,
or must I explicitly initialize it to LUA_NOREF (which happens to be
-2) ?

(It's not that I'm being lazy. In reality I have several huge arrays
of references (allocated and zero'ed with calloc()), not just one
variable, and I want to skip the loops that are to initialize them.)

A bonus question: if it's ok to assume luaL_ref() won't return 0, then
why does LUA_NOREF equal -2 instead of simply being 0 ?


the manual says:

    A reference is a unique integer key.
    As long as you do not manually add integer keys into table t, luaL_ref ensures the uniqueness of the key it returns.
    The constant LUA_NOREF is guaranteed to be different from any reference returned by luaL_ref.


all luaL_ref can guarantee is the uniqueness. current implementation use only positive integer keys.

Lua 5.3 use index 0 as a special key to keep track the unref-ed slots as a linked free list.

but it should be considered as implementation detail I think.


--
the nerdy Peng / 书呆彭 / Sent from Thunderbird