Lua中值的最小大小是多少?例如一个数字。
在计算值数组的大小时,此知识特别有用。
答案 0 :(得分:1)
这是在Lua 5.3中定义Value的方式:
#define TValuefields Value value_; int tt_
/*
** Tagged Values. This is the basic representation of values in Lua,
** an actual value plus a tag with its type.
*/
/*
** Union of all Lua values
*/
typedef union Value {
GCObject *gc; /* collectable objects */
void *p; /* light userdata */
int b; /* booleans */
lua_CFunction f; /* light C functions */
lua_Integer i; /* integer numbers */
lua_Number n; /* float numbers */
} Value;
#define TValuefields Value value_; int tt_
typedef struct lua_TValue {
TValuefields;
} TValue;
GCObject
的定义如下:
/*
** Common type for all collectable objects
*/
typedef struct GCObject GCObject;
/*
** Common Header for all collectable objects (in macro form, to be
** included in other objects)
*/
#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
/*
** Common type has only the common header
*/
struct GCObject {
CommonHeader;
};
/* chars used as small naturals (so that 'char' is reserved for characters) */
typedef unsigned char lu_byte;
/*
** Type for C functions registered with Lua
*/
typedef int (*lua_CFunction) (lua_State *L);
/* type for integer functions */
typedef LUA_INTEGER lua_Integer;
LUA_INTEGER
depends on the platform and build settings的定义,但通常是64位有符号整数。
/* type of numbers in Lua */
typedef LUA_NUMBER lua_Number;
LUA_NUMBER
也取决于配置和平台,但通常是double
。
因此,要获得最小大小,您必须计算以下内容(假设所有指针类型具有相同的长度):
max(sizeof(pointer), sizeof(int), sizeof(LUA_INTEGER), sizeof(LUA_NUMBER)) + sizeof(int) + padding
在x86_64上,这通常是:
sizeof(pointer) + sizeof(int) + padding = 8 + 4 + 4 = 16
32位x86:
sizeof(double) + sizeof(int) + padding = 8 + 4 + 4 = 16