Purpose of memory alignment

前端 未结 8 1296
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 05:20

Admittedly I don\'t get it. Say you have a memory with a memory word of length of 1 byte. Why can\'t you access a 4 byte long variable in a single memory access on an unalig

相关标签:
8条回答
  • 2020-11-22 06:01

    If a system with byte-addressable memory has a 32-bit-wide memory bus, that means there are effectively four byte-wide memory systems which are all wired to read or write the same address. An aligned 32-bit read will require information stored in the same address in all four memory systems, so all systems can supply data simultaneously. An unaligned 32-bit read would require some memory systems to return data from one address, and some to return data from the next higher address. Although there are some memory systems that are optimized to be able to fulfill such requests (in addition to their address, they effectively have a "plus one" signal which causes them to use an address one higher than specified) such a feature adds considerable cost and complexity to a memory system; most commodity memory systems simply cannot return portions of different 32-bit words at the same time.

    0 讨论(0)
  • 2020-11-22 06:01

    On PowerPC you can load an integer from an odd address with no problems.

    Sparc and I86 and (I think) Itatnium raise hardware exceptions when you try this.

    One 32 bit load vs four 8 bit loads isnt going to make a lot of difference on most modern processors. Whether the data is already in cache or not will have a far greater effect.

    0 讨论(0)
提交回复
热议问题