natevw/struct-fu

Byte allignment issue

Closed this issue · 2 comments

Hi,
From my experience, I think struct-fu supports 1 byte alignment ( pack1 ), so are there any way that I can make struct-fu structure compatible with variable byte alignment ( like considering the compiler or by any options)
From my observations,

struct UpdateCustomer
{
int CustID;
cics_char_t CustName;
float customerLastBillAmount;
};

This is the structure I used, in C I got the sizeof(struct UpdateCustomer) equal to 12 (4 Byte alignment) but in node.js struct-fu, UpdateCustomer.size = 9 ( 1 Byte alignment)

You are correct. At the time I figured this would vary so much between compilers that it would be difficult to "keep up" with the possibilities. But it looks like many compilers are consistent, at least for x86. Embedded is a key use case of this though too, I wouldn't be surprised if compilers there vary more.

So at some point this might be a nice feature to add (probably by way of a new option for _.struct), if it doesn't add too much complexity and maintenance burden. For now, though we have as the documentation states:

No hidden/implicit padding or alignment (WYSIWYG)

For now I'd recommend simply adding fields like e.g. _.byte('_pad1', 3) where necessary to make things match. There's also the _.padTo(offset) field which won't create extra buffers, but that requires a bit more manual calculation since it's an absolute offset ("make the whole struct X bytes long") rather than a relative one ("add three bytes here"). Perhaps sooner than complete "automatic" padding support, a relative field _.padding(size) would be useful, then?

I've filed two followup issues #13 and #14 to better capture some ideas that could help here. Closing this thread since I think I had already answered your question. Feel free to reopen if you still have questions/concerns!