Since we can address every byte of memory individually, why do compilers take extra care to make sure that structs and it’s members align to 32-bit borders in memory?
I could be wrong here, but on a 32-bit system, is it not just as fast to get 4 bytes starting from say 0x0800, as it is from 0x0801?
Since we can address every byte of memory individually, why do compilers take extra
Share
On most architectures it is faster to perform read/write on naturally aligned data types. On some systems it will generate an exception (i.e. crash in most cases) if you try to access certain types when they are misaligned. So in general you always want to maintain natural alignment unless you have a very good reason not to.
See also related SO questions and answers:
Purpose of memory alignment
why is data structure alignment important for performance?
Does unaligned memory access always cause bus errors?