结构对齐规则的目的 [英] Purpose of Structure Alignment Rules

查看:79
本文介绍了结构对齐规则的目的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道为什么引入了结构对齐规则。

有/无边界对齐的结构有何不同。


问候

Swapna

Hi,
I would like to know why structure alignment rules were introduced.
How does a structure with/without boundary alignments differ.

Regards
Swapna

推荐答案

使处理器更容易,更快地访问数据。


所有处理器都具有本机数据总线宽度,并且(通常)从内存中读取与此宽度对齐的数据。例如,如果我有一个32位处理器,那么它将读取与32位对齐的内存位置。


如果我从内存读取一个32位整数,如果该整数在32位(4字节)边界上对齐,则可以用一条指令读取数据。但是,如果32位整数未在32位边界上对齐,则就处理器而言,数据分布在2个位置。它必须读取两个位置,它们将通过丢弃不需要的数据读取的2个值组合起来,然后移动剩余的内容以重新创建32位值,并获取多个处理器指令。


结构(和类)被打包,以便它们的成员出现在边界上,使处理器可以轻松访问。在某些平台上,这可以被覆盖,从而迫使结构填充到不太理想的值。这样可以有效地使用数据(没有浪费的填充字节),但访问速度较慢。
To make it easier and faster for the processor to access the data.

All processors will have a native data bus width and (normally) read data from memory aligned to this width. If, for instance, I have a 32 bit processor then it will read memory location aligned to 32 bits.

If I am reading a 32 bit integer from memory if that integer is aligned on a 32 bit (4 byte) boundry then the data can be read with a single instruction. However if the 32 bit integer is not aligned on a 32 bit boundary the data is spread across 2 locations as far as the processor is concerned. It would have to read both locations and them combine the 2 values read by discarding unwanted data and shifting what is left to re-create the 32 bit value take several processor instructions.

So structures (and classes) are packed so that their members appear at boundaries that make it easy for the processor to access. On some platforms this can be overridden forcing structure packing to a less optimal value. This results in moe efficient use of data (no wasted padding bytes) but slower access to the data.




解释很好理解。谢谢。

但填充字节会产生''漏洞'并阻止内存使用。你对此有什么看法?


问候

Swapna
Hi
Explanation well understood. Thanks.
But padding of bytes create ''holes'' and block memory usage.What do u have to say on this?

Regards
Swapna





解释很好理解。谢谢。

但填充字节会产生''漏洞'并阻止内存使用。你对此有什么看法?


问候

Swapna
Hi
Explanation well understood. Thanks.
But padding of bytes create ''holes'' and block memory usage.What do u have to say on this?

Regards
Swapna



一般来说,与今天的内存系统数量相比,这种浪费相对较低(每个结构几个字节)。因此,除非你有很多这样的结构,否则浪费可能是微不足道的。


如果你有大量的结构或有限的记忆,那么减少这种浪费可能是明智的。你可以通过仔细制作你的结构来做到这一点,举个例子

Generally speaking this wastage is relatively low (a few bytes per structure) compared to the amount of memory systems tend to have today. So unless you have a lot of those structures the wastage is probably negligible.

If you have a very large number of structures or limited memory then reducing this wastage may be sensible. You can do this by crafting your structures carefully, take this example

展开 | 选择 | Wrap | 行号


这篇关于结构对齐规则的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆