2
votes

Let the following code :

lua_pushinteger( m_pLuaState, 123 );
const char * sAsString = lua_tostring( m_pLuaState, -1 );

The returns non-NULL pointers to strings containing strange numeric values like "17137503626592379" or "14326121113845883" etc...

It seems random, does not violate memory (at least in a few tries), and the returned strings always contain valid numeric values despite not being the one I'm passing in the first place (i.e 123).

I assume this has to do with floating point / integer convertions.

I'm using Lua 5.3.2, which supports both "integer" and "float" as being two subtypes of "number", and i'm using floats for numbers and int for integers. I'm using both Windows CE and Windows 7. On both platforms "int" and "float" are both 32 bits, despite my Windows 7 being 64 bits, because I'm explicitely compiling for x86.

Here is a piece of my luaconf.h

#if !defined(LUA_INT_TYPE)
#define LUA_INT_TYPE    LUA_INT_INT
#endif

#if !defined(LUA_FLOAT_TYPE)
#define LUA_FLOAT_TYPE  LUA_FLOAT_FLOAT
#endif

While writing this I realized that it might be due to the fact that I changed the types being used without recompiling the static lua .lib that i'm linking against. Could this be due to this ? I will try recompiling...

Could this be due to something else ? Thank you.

2

2 Answers

2
votes

Well after recompiling the lua53.lib once the types to be used for integer and floats were defined, it works properly.

The problem is that I changed these types without recompiling the library. The Windows desktop version was initally compiling using doubles instead of floats. I guess this is the origin of the problem.

0
votes

I answer since I cannot comment, but what I see in your code.

lua_pushinteger( m_pLuaState, 123 );
const char * sAsString = lua_tostring( m_pLuaState, -1 );

you are pushing and integer and pulling a string, since they are different data types, I see normal that it give you funny outputs. I suppose that is trying to translate the information of the stack from integer to string.