lua-users home
lua-l archive

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


On Mon, Dec 13, 2010 at 09:30:53AM +0200, Krawtschunowski, Wladimir wrote:
> Hi, all!
> Does lua reserve any memory for saving keys of tables, if the values are all nil? Assume we have the code:
> -----------
> a={}; b={}; c={}
> a[1000]=1
> b[1] = 2; b[1000] = 1; b[1] = nil
> c[1] = 11
> -----------
> The question is, if lua allocates memory for the 1. until 999. numerical 
> keys of the table a (because the first valid key is 1000), or not=? 
> Is after the execution any difference between the tables a and b?

We have a few hours before the working day start aross the ocean in Rio, 
so I'll stick my neck out ...

The table 'a' will have an array part of length 0, and 'b' will
have an array part of length 1 when you say 'b[1]=2'.  From ltable.c:
    The actual size of the array is the largest `n' such that at
    least half the slots between 0 and n are in use.
The array part will therefore stay at length 1 when you say "b[1000]=1".

When you say "b[1]=nil" the array part should drop back to length 0,
according to that statement, but whether the memory already allocated
is freed just yet I don't know.

Dirk