English 中文(简体)
因此,结构的规模需要成为任何结构成员最大组合的多重部分。
原标题:why does size of the struct need to be a multiple of the largest alignment of any struct member

我理解在确保正确协调各种类型的结构成员之间发生的争.。 然而,为什么数据结构必须成为最大成员的多重组合? 我根本不理解最后的dding。

Reference: http://en.wikipedia.org/wiki/Data_structure_alignment

最佳回答

良好问题。 考虑这种假设类型:

struct A {
    int n;
    bool flag;
};

因此,<代码>A的物体应使用5个 by子(4个 in子加1个ool),但实际上需要8个。 为什么?

如果你使用这样的类型,就会看到答案:

const size_t N = 100;
A a[N];

如果每个<代码>A仅限5英特,则a[0]将加以调整,但a?]>,a],其他大部分内容将不予调整。

但为什么协调甚至重要? 有几个原因,所有与硬件有关。 其中一个原因是,最近/经常使用的记忆在CPU硅的cache Line上,以便迅速进入。 一个比切线小的附合物体总是适合单一线(但见下文所附有趣的评论),但一个不结盟物体可能跨越两条线,穿孔。

事实上,更根本的硬件原因是,必须采用可加处理的数据方式,把32至64轨的数据公共汽车,与切线相距甚远。 不仅会错误地将公共汽车与外壳混为一谈(如同以前一样是跨界的),而且还会迫使登记册在出现时转移。 更糟糕的是,误导往往混淆优化逻辑(至少,《英特尔优化手册》指出,它确实如此,尽管我对最后一点没有个人了解)。 因此,从业绩角度看,错失行为非常糟糕。

由于这些原因,通常会浪费tes子。

Update: 以下评论都是有用的。 我建议这些建议。

问题回答

视硬件而定,可能需要调整或仅仅帮助加快执行。

There is a certain number of processors (ARM I believe) in which an unaligned access leads to a hardware exception. Plain and simple.

尽管典型的x86加工商更为宽松,但在获得不结盟的基本类型方面仍会受到处罚,因为加工商必须做更多的工作,在能够进行注册之前,才能将轨道列入登记册。 但是,在 包装<>/em>时,供应商通常提供特定特性/具体特性。

由于虚拟处理。

"...aligning a page on a page-sized boundary lets the hardware map a virtual address to a physical address by substituting the higher bits in the address, rather than doing complex arithmetic."

顺便提一下,我发现Wikipedia网页的文字非常好。

如果万国邮联的登记面积为32比特,那么它就可以 gr住32比特边界的、单一的组装指示。 放牧速度较慢,达到32个轨道,然后从8个轨道开始。

BTW:确实有禁.。 你们可以要求包装结构。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签