lua-users home
lua-l archive

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

2009/2/21 Bertrand Mansion <>:
> I am trying to create a C module for Lua. One of the functions deals
> with string quoting so I need to allocate a buffer with 2 times +1 the
> memory occupied by the source string. Usually, I would use malloc()
> but I noticed that there is a lmem.h file with a luaM_malloc()
> function, so I could write something like this:
>  to = luaM_malloc(L, 2 * size);
> Unfortunately, #include "lmem.h" doesn't work if I install Lua using
> the default Makefile (I am on Macosx). Is there a way to build Lua or
> some extra steps I should do manually in order to be able to use
> "lmem.h" ? Thanks,

You have two normal ways to allocate memory with Lua. You can either
call lua_getallocf to retrieve Lua's memory allocator and use it. You
can also create a userdata on the stack with lua_newuserdata. The
interesting aspect of using a userdata is that it is garbage
collected, and you don't have to take care of de-allocation, even in
the case of Lua errors.