What is “Alignment” field in binary formats? Why is it needed?

北城以北 提交于 2019-12-07 09:38:52

问题


In ELF file format we have an Alignment field in Segment Header Table aka Program Header Table.

In case of Windows PE file format they take it to next level the Sections have two alignment values, one within the disk file and the other in memory. The PE file header specifies both of these values.

I didn't understand a thing about this alignment. What do we need it for? How & Where is it used? Again, I don't know what is alignment in binary file format context but why do we need it?


回答1:


Well, alignment is usually stretching the storage size of some value to occupy some "round" space, like 32, 64, 128 bit etc.

If we're talking about binary formats, it may be done in order to optimize format processing. Read/write operations can be quicker when you read/write some "round" data length portions.

I found a reading for you, formulated in better words I can come up with right now:

Data structure alignment

Data structure alignment is the way data is arranged and accessed in computer memory. It consists of two separate but related issues: data alignment and data structure padding. When a modern computer reads from or writes to a memory address, it will do this in word sized chunks (e.g. 4 byte chunks on a 32-bit system). Data alignment means putting the data at a memory offset equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory. To align the data, it may be necessary to insert some meaningless bytes between the end of the last data structure and the start of the next, which is data structure padding.



来源:https://stackoverflow.com/questions/2391865/what-is-alignment-field-in-binary-formats-why-is-it-needed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!