From what I have read, misaligned access means mostly two things:
- you may get a performance loss
- you will lose atomicity of loads and stores that aligned access has
Supposing that performance is not an issue and what I want from software is correctness, how bad is misaligned access? My understanding is that the x86 CPU will handle such accesses correctly but may have to do additional work to fetch the data.
What lead me to asking this question was compiling my code with -fsanitize=undefined
. I got many errors about misaligned stores/loads. I am not sure if this is an issue because:
- the stores are performed only during data preparation which is a single-threaded process, so I am not concerned about loss of atomicity
- the loads are performed in a multithreaded process where many threads (four or more) access the data, but the data is not modified by any of them (held in a
const uint8_t*
variable)
The reason accesses are not aligned is that the const uint8_t*
array contains bytes from many different types (uint8_t
, uint16_t
, uint32_t
, uint64_t
, and int64_t
).
I am sure that no load goes outside the bounds of the allocated uint8_t
array (e.g. the program never loads uint64_t
from an address that points to last one, two, or three bytes of the allocated memory block), and am sure that my accesses are all correct - only misaligned.
Another thing I read is that such loads may be breaking the strict aliasing rules, but the code compiles without a single warning with -Wstrict-aliasing -Werror
(which I have long ago enabled).
Should I pad the data in the uint8_t
array to ensure accesses are aligned, or may I safely ignore the warnings?