Re: Unaligned memory access

Spar <[email protected]>
Newsgroups gmane.comp.lang.lua.general
Message-ID <a069fe44-3d63-410c-8bc3-0afc797d497e@Spark>
You can also look at std::align or how sol2 manages this: https://github.com/ThePhD/sol2/blob/develop/include/sol/stack_core.hpp#L129
On Feb 14, 2026 at 21:31 +0300, Andre Leiradella <[email protected]>, wrote:
> Em sábado, 14 de fevereiro de 2026 às 14:00:40 UTC, Sainan escreveu:
> > lua_newuserdata does not provide alignment guarantees, but practically, you will see that it is only 8-byte aligned (on 64-bit). Of course, your workaround of using 'new' works, but you can also simply allocate 15 more bytes so you can safely offset the pointer:
> >
> > char *data = reinterpret_cast<char*>(lua_newuserdata(L, data_len + 15));
> > if (reinterpret_cast<uintptr_t>(data) % 16) { /* data is not 16-byte aligned? */
> > data = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(data) + (16 - (reinterpret_cast<uintptr_t>(data) % 16))); /* align data to 16-byte boundary */
> > }
> >
> > -- Sainan
>
> Good idea, I ended up using it instead of the additional pointer indirection:
>
> ud = (ud + 15) & ~(uintptr_t)15;
>
> Andre
> --
> You received this message because you are subscribed to the Google Groups "lua-l" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
> To view this discussion visit https://groups.google.com/d/msgid/lua-l/7d247e5e-5c1d-4ab6-91bf-52c305546e02n%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/a069fe44-3d63-410c-8bc3-0afc797d497e%40Spark.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.