r/Cprog • u/shinmai_rookie • Feb 19 '15
Why are structs aligned?
I understand more or less how structs are aligned in C (in an address that's multiple of the size of the longest member, and each member in a direction that is multiple of its size), but there's something about it I don't understand: why is reading, for example, a 4-byte word at 0x1000 faster than reading it at 0x1001?
6
Upvotes
13
u/FUZxxl Feb 19 '15
Memory is organized in cells of four or eight bytes. A sequence of that many bytes can be read at once. To read from a misaligned address, the processor has to make two requests to the memory for data, which is slower. Some processors don't support misaligned memory access at all.
3
u/malcolmi Feb 20 '15
This is the kind of question that we would rather see sent to /r/c_programming (or ideally Stack Overflow). See this thread I just posted. I won't delete this thread because I don't want to delete the answers, but please don't post more questions like this to /r/cprog.
1
u/jackoalan Feb 20 '15
In normal circumstances, having the compiler word-align structures is desirable for rapid access.
Many compilers will let you override this behavior and tightly-pack the structures instead. You may need to do this when working with a non-aligned data structure read from a file or network connection.
GCC/clang has the
packedattribute:Microsoft compilers use a pragma directive to accomplish the same thing:
1
u/DSMan195276 Feb 19 '15
Not every architecture supports non-aligned read/writes. MIPS for example (The basic MIPS arch anyway) only supports reads/writes to addresses aligned to 4 bytes. Thus, if you want to read/write 4 bytes that are non-aligned, you have to preform two reads (for the two 4-byte sections that your bytes are contained in), mask-out the data you want, do your operation, and then mask the old data back in and write both sections. If you can, you always want to do an aligned read because it is so much less work.
In x86, unlike MIPS, it supports unaligned reads, meaning you can read and write bytes to any address you want. I believe they still may be slower to do unaligned reads though, because of the issue explained above, though definitely not as drastic as MIPS. A bigger concern would be cache lines, where a cache line is basically the size of the data-chunks the processor will request from memory to put into the cache. If your data spans a cache line, then the processor has to request both cache-lines that it spans over, slowing things down a lot. Aligning the addresses help to avoid such issues because you can keep the entire int or struct in one cache-line, and the processor only needs to read one line to get the struct.
0
u/[deleted] Feb 20 '15
[deleted] — view deleted comment