lua-users home
lua-l archive

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

Roberto wrote:

> Yes; Lua gives no garanties about alignment of its strings (except that
> they are valid for strings ;-).

Somehow this seems unfortunate.  Lua is supposed to interface with C well.  Yet it cannot load
binary data for C without converting it to a userdata type which requires a memory copy.  Then
if you want Lua to save the data back out, you need to convert back to a string with another
memory copy.  I guess the read/write functions can be rewritten for some userdata type, but is
it really such a stretch for Lua to align buffers as malloc does?  The argument of wasting space
cannot be used since TString already has padding to match malloc's alignment.

(By the way, according to ANSI C malloc's "allocated space is suitably aligned for storage of
any type of object".)

> Wouldn't you have to handle alignment details anyway? For instance, if
> you pack with format "cf", the floating point will not be aligned.

This is not related to the issue of the start alignment of the binary data.  Of course the
person writing the template needs to know exact the structure he's working with, including
alignment details.  There is 'x' for byte padding.  If floats are 4-byte aligned you would write