什么是“对齐”?二进制格式的字段?为什么需要它?

发布于 2024-08-24 11:39:15 字数 281 浏览 6 评论 0原文

ELF文件格式中,我们在段头表(又名程序头表)中有一个对齐字段。

对于 Windows PE 文件格式,它们将其提升到下一个级别,这些部分有两个对齐值,一个在磁盘文件中,另一个在内存中。 PE 文件头指定这两个值。

我对这种对齐方式一无所知。我们需要它做什么?如何&它用在哪里?再说一次,我不知道二进制文件格式上下文中的对齐是什么,但为什么我们需要它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弃爱 2024-08-31 11:39:15

好吧,对齐通常是拉伸某些值的存储大小以占用一些“圆形”空间,例如 32、64、128 位等。

如果我们谈论二进制格式,则可能会这样做以优化格式处理。当您读/写一些“圆形”数据长度部分时,读/写操作可能会更快。

我为您找到了一本读物,用我现在能想出的更好的词语来表述:

数据结构对齐

数据结构对齐是数据在计算机内存中排列和访问的方式。它由两个独立但相关的问题组成:数据对齐数据结构填充。当现代计算机读取或写入内存地址时,它将以字大小的块(例如,32 位系统上的 4 字节块)执行此操作。 数据对齐意味着将数据放置在等于字大小倍数的内存偏移处,这会由于CPU处理内存的方式而提高系统的性能。为了对齐数据,可能需要在最后一个数据结构的末尾和下一个数据结构的开头之间插入一些无意义的字节,这就是数据结构填充

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文