CPU and Data alignment

后端 未结 7 1885
逝去的感伤
逝去的感伤 2020-11-29 06:22

Pardon me if you feel this has been answered numerous times, but I need answers to the following queries!

  1. Why data has to be aligned (on 2-byte / 4-byte / 8

相关标签:
7条回答
  • 2020-11-29 06:46

    Very little data "has" to be aligned. It's more that certain types of data may perform better or certain cpu operations require a certain data alignment.

    First of all, let's say you're reading 4 bytes of data at a time. Let's also say that your CPU has a 32 bit data buss. Let's also say your data is stored at byte 2 in the system memory.

    Now since you can load 4 bytes of data at once, it doesn't make too much sense to have your Address register to point to a single byte. By making your address register point to every 4 bytes you can manipulate 4 times the data. So in other words your CPU may only be able to read data starting at bytes 0, 4, 8, 12, 16, etc.

    So here's the issue. If you want the data starting at byte 2 and you're reading 4 bytes, then half your data will be in address position 0 and the other half in position 1.

    So basically you'd end up hitting the memory twice to read your one 4 byte data element. Some CPUs don't support this sort of operation (or force you to load and combine the two results manually).

    Go here for more details: http://en.wikipedia.org/wiki/Data_structure_alignment

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