[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Question about erasing tables
- From: J Doe <general@...>
- Date: Mon, 3 Jul 2017 14:47:12 -0400
Hi,
I have a rather basic question regarding "erasing" tables (preparing them to be GC'ed), from reading "Lua Gems". Not sure why, but I'm having trouble understanding a code example and I figured the Lua list was the place to bring it (everyone on this list is always so good at responding!).
In Chapter 2, Roberto mentions that "If you want to erase all elements from a table, a simple traversal is the correct way to do it:
for k in pairs(t) do
t[k] = nil
end
Roberto then goes on to mention an improvement to this:
while true do
local k = next(t)
if not k then break end
t[k] = nil
end
What I'm not understanding is that the second code sample suggests to me that it iterates over the table and sets each element to nil. However, I am assuming that if there is a nil in the table, this satisfies "if not k then break end", which would mean that a table with one or more nils will not have *all* of its' elements set to nil and therefore will not be reclaimed by the GC.
Is it possible that Roberto is referring to a way of shrinking the table in the second example, as opposed to preparing the *entire* table for reclamation ?
Thanks for your help,
- J