Here I’ve found that if raw_input_buffer allocated on stack it is not aligned correctly, making program crash on @alignCast.
I would like to understand if it is expected behaviour. u8 is aligned to 1, but I was expecting that stack space for array still will be aligned to architecture word size.
Zig Version: 0.17.0-dev.314+eae06cf5c
As workaround I’ve put raw_input_buffer to global variable and it resolved the problem. I’m actually wondering, maybe this is more appropriate place for such buffers. Still, would like to know the situation with the stack.
The only time alignment will be in the type is with pointers/slices e.g []align(8) u8. That will ofc align the data pointed to and not the place the pointer is stored.
The funny thing is that this alignment problem never caused problems, but was found via Zig because that compiles C code with ASAN in debug mode.
I think the ‘correct’ solution is to allocate the input buffer on the heap, e.g. first call GetRawInputData with a NULL buffer pointer to query the required size, then use that size to allocate a heap buffer, then call GetRawInputData() again with the allocated buffer pointer to get the actual data (in Zig you might want to allocate with the required alignment, in C MSVC malloc guarantees 16-byte alignment, which is good enough).
This is the approach that GLFW is using, and which since then I also use in sokol_app.h.
PS: the problem with this fixed-size buffer raw_input_buffer: [0xFF] u8 is also that theoretically GetRawInputData() may want to return more data than 255 bytes (in reality it doesn’t, but some weird input devices might theoretically).