lua-users home
lua-l archive

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



On Sun, Apr 7, 2019 at 3:17 AM Marc Balmer <marc@msys.ch> wrote:
Is it allowed to modify strings returned by lua_tolstring, luaL_checkstring etc.? (after casting to a char *).

Or does that have any negative side effects?

Lua stores only one copy of each string if it's below a certain length, this speeds up lookups when using strings as table indices tremendously, and saves some space too. So if you change the contents of the string you may see some very strange side effects, like "foo" and "foo" being unequal strings.

You really should use a userdata object as a scratch buffer in C routines, for two reasons:
  • the Lua interpreter does not care about the contents, so you can use it for anything.
  • if you call a Lua C API function that produces an error which terminates your function the function's stack frame is discarded and the userdata memory is freed eventually. If you use malloc/free that won't happen unless you use lua_pcall/lua to catch the error and explicitly free the memory in your code.


--
--