CPU and Data alignment

后端 未结 7 1905
逝去的感伤
逝去的感伤 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:43

    In general, the one answer to all three of those questions is "it depends on your system". Some more details:

    1. Your memory system might not be byte-addressable. Besides that, you might incur a performance penalty to have your processor access unaligned data. Some processors (like older ARM chips, for example) just can't do it at all.

    2. Read the manual for your processor and whatever ABI specification your code is being generated for,

    3. Usually when people refer to data being at a certain alignment, it refers only to the first byte. So if the ABI spec said "data structure X must be 4-byte aligned", it means that X should be placed in memory at an address that's divisible by 4. Nothing is implied by that statment about the size or internal layout of structure X.

      As far as your particular example goes, if the data is 4-byte aligned starting at address 1004, the next byte will be at 1005.

提交回复
热议问题