English 中文(简体)
2. 记忆调整和结构规模
原标题:Memory alignment and size of structure

Let s say that I define the following structure:

struct MyData { 
    int a;
    char b;
    int c;
    byte d;
    byte e;
}

我含糊地记住,这种结构的规模不仅取决于数据类型,而且取决于记忆的一致。 在32个轨道上,MyData公司的结构是4个 by子+1个 by子+4个 by子+1个 by子=11个 by子。 这里我的问题是,记忆调和增加了结构的规模:4个 by+1个 by(+3个 by)+4个 by+1个 by(+3个 by)+1个 by(+3个 by)=20个 by。

这是否是错误的? 我失踪了吗? 这一措辞是否具体? 我能否包装结构? 如果是的话,什么是好处和不利条件?

感谢!

问题回答

汇编者可按其认为合适的方式铺设结构。 通常,最后两个<代码>bytes 不会通过婚礼分离,因此其大小将变成<代码>4(int) + 1 (char) + 3 (pavention) + 4 (int) + 1 (byte) + 1 (byte) + 2 (patter) = 16。

许多汇编者允许每块rag块包装。 其优点是记忆使用较少,但不利于不结盟的<条码>(int)成员。

您指出,记忆的调整可以提高结构的规模,这并不错;然而,对于如何协调记忆,任何格言对于所有平台都无效。 这纯粹是具体的平台。

基本上,大多数平台倾向于按美元=1美元(WORDSIZE}计算,或者如果数据类型小于1美元(WORDSIZE}),那么它就可调取的下一个部分(iWORDSIZE})。

例如,如果你有32个字眼,而且你正在储存16个字节短,他们可以在一字内对0和16个字眼表示赞成。 但这不是一种保障,因为它确实是具体的平台。

打破你因dding而形成的小废物结构,按数据类型排列,数据类型越大,数据类型越多。 这往往允许多口特混为同一字(如果可能的话),而超过字数的物项在字面上会明显终止,因为它们往往是一字数的清洗(序号、两字......)

在建筑成员之间和之后,有unspecised pa。 第一层建筑成员之前,即:

struct MyData bla;

int val = (char *) &bla == (char *) &bla.a;  // val is 1

结构的一个要点(适当转换)指第一个结构成员。

结构标的大小考虑到婚礼,等于成员人数之和,加上未指明的婚礼的大小。

是的,汇编者自然会根据与其规模相称的边界调整类型。 你们可以使用编织物(如编织物)强迫包装

#pragma pack(1)

你们也可以避免一些dding子,重新定下你的声明,把你的 in放在一开始,然后单独挑出。

可以通过印刷<条码>(图形MyData)易于测试。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

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->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签