Re: Unaligned memory access

Andre Leiradella <[email protected]>
Newsgroups gmane.comp.lang.lua.general
Message-ID <[email protected]>
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.
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.