结构的大小:结构填充 [英] Size of a structure : Structure Padding

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

问题描述

案例1:

struct s

{

char c1 [6]; // 8

加倍; // 8

int i1; // 4

char c2 [2]; // 4

int i2; // 4

};


案例2:

struct s

{

char c1 [6]; // 8

加倍; // 8

int i1; // 4

char c2 [2]; // 4

int i2; // 4

float f // 4

};


根据结构填充的规则不应该是上面结构的大小是28字节(对于32位编译器,我使用的是DEV-C +

+)?但是我使用sizeof得到了32作为答案。当我添加

" float f"对于结构,我得到了相同的答案,32。

好​​吧,如果在第一种情况下32是正确而不是28,那么在

之后添加一个浮点数,不应该它变成了36?但即使在案例2中,它的价格为b
。是否有更多的结构填充,而不是简单的俱乐部一起

4组?有人请说清楚吗?

解决方案

10月1日上午10点16分,Kislay< kislaychan ... @ gmail。 comwrote:


case 1:

struct s

{

char c1 [6]; // 8

加倍; // 8

int i1; // 4

char c2 [2]; // 4

int i2; // 4


};


案例2:

struct s

{

char c1 [6]; // 8

加倍; // 8

int i1; // 4

char c2 [2]; // 4

int i2; // 4

float f // 4

};


根据结构填充的规则不应该'

以上结构的大小为28字节



结构填充的唯一规则是填充可能发生

结构内的任何地方,除了它的开头。如果这个

结构是100字节,那仍然可以


但我得到32作为答案,使用sizeof 。当我添加

" float f"对于结构,我得到了相同的答案,32。

好​​吧,如果在第一种情况下32是正确而不是28,那么在

之后添加一个浮点数,不应该它变成了36?



不一定,第一个结构可能有四个字节的

填充,而第二个结构可能没有
< blockquote class =post_quotes>
是否有更多的结构填充而不是简单的俱乐部一起

4组?



没有要求俱乐部一起任意数量的字节。

的实现是免费的,可以按照自己的意愿行事,但通常它会根据机器的对齐要求提供




问候,

B.


Kislay写道:


案例1:

struct s

{

char c1 [6]; // 8

加倍; // 8

int i1; // 4

char c2 [2]; // 4

int i2; // 4

};


案例2:

struct s

{

char c1 [6]; // 8

加倍; // 8

int i1; // 4

char c2 [2]; // 4

int i2; // 4

float f // 4

};


根据结构填充的规则不应该是上面结构的大小是28字节(对于32位编译器,我使用的是DEV-C +

+)?



这两个中的哪一个是上述结构?


但不是:它真的不是物。结构的大小

是编译器选择投入的内存量。

不同的编译器做出不同的选择,因为它们是

关注不同的约束 - 更一般地说,

不同的品质因数。


但我得到了32回答,使用sizeof。



...这是正确答案;好样的! (当然,

不同的编译器可能给出了不同的答案,

*也*是正确答案; bravo!)


当我添加

" float f"在结构上,我得到了同样的答案,32。



另一个正确答案!你在滚动。


好​​吧,如果在第一种情况下32是正确而不是28,那么在

之后添加一个浮,不应该变成36?但即使在案例2中,它的价格为b
。是否有更多的结构填充,而不是简单的俱乐部一起

4组?请有人解释一下吗?



假设:在您目前使用的系统上,

编译器会尝试确保每个双重开始于内存

地址(如果被视为数字)可以被8整除。

如果假设是正确的,它是否暗示了

对齐要求的任何内容对于一个结构,它的成员是一个双重的

?此外,struct的对齐要求

对其sizeof有任何影响吗?提示:在`struct s array [2];''

考虑地址`& array [0]''和`& array [1]''。


-

Eric Sosman
es **** *@ieee-dot-org.inva


Kislay写道:


>

案例1:

struct s

{

char c1 [6]; // 8

加倍; // 8

int i1; // 4

char c2 [2]; // 4

int i2; // 4

};


案例2:

struct s

{

char c1 [6]; // 8

加倍; // 8

int i1; // 4

char c2 [2]; // 4

int i2; // 4

float f // 4

};


根据结构填充的规则不应该是上面结构的大小是28字节(对于32位编译器,我使用的是DEV-C +

+)?但是我使用sizeof得到了32作为答案。当我添加

" float f"对于结构,我得到了相同的答案,32。

好​​吧,如果在第一种情况下32是正确而不是28,那么在

之后添加一个浮点数,不应该它变成了36?但即使在案例2中,它的价格为b
。是否有更多的结构填充,而不是简单的俱乐部一起

4组?请有人解释一下吗?



看起来你的双打需要与一个地址对齐

是零模8.现在解释一个两个数组的数组案例1结构如果结构大小为28,那么
可以做到这一点吗?即他们需要填充额外的4个字节来填充
。你添加的浮动只是使用那个

的填充空间。


-

Chuck F(cinefalconer at maineline dot net)

适用于咨询/临时嵌入式和系统。

< http://cbfalconer.home.att.net>

-

通过 http://www.teranews.com


case 1 :
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
};

case 2:
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
float f // 4
};

According to the rules of structure padding shouldn''t the size of the
above structure be 28 bytes (for a 32 bit compiler , I am using DEV-C+
+) ? But I got 32 as the answer , using sizeof . And when I added
"float f" to the structure , I was getting the same answer , 32 .
Well , if 32 is right instead of 28 in the first case , then after
adding a float , shouldn''t it become 36 ? But even in case 2 , its
32 . Is there more to structure padding than simple clubbing together
in groups of 4 ? Somebody please shed some light ?

On Oct 1, 10:16 am, Kislay <kislaychan...@gmail.comwrote:

case 1 :
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4

};

case 2:
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
float f // 4

};

According to the rules of structure padding shouldn''t the size of the
above structure be 28 bytes

The only rules for structure padding is that padding may occur
anywhere within the structure, except at its beginning. If this
structure was 100 bytes, that would be still OK

But I got 32 as the answer , using sizeof . And when I added
"float f" to the structure , I was getting the same answer , 32 .
Well , if 32 is right instead of 28 in the first case , then after
adding a float , shouldn''t it become 36 ?

Not necessarily, the first structure may have had four bytes of
padding, and the second structure may have none

Is there more to structure padding than simple clubbing together
in groups of 4 ?

There is no requirement to "club" together any number of bytes. The
implementation is free to do what it wants, but usually it pads based
on the alignment requirements of the machine.

Regards,
B.


Kislay wrote:

case 1 :
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
};

case 2:
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
float f // 4
};

According to the rules of structure padding shouldn''t the size of the
above structure be 28 bytes (for a 32 bit compiler , I am using DEV-C+
+) ?

Which of the two is "the above structure?"

But no: it really doesn''t matter. The size of the struct
is the amount of memory the compiler chooses to devote to it.
Different compilers make different choices, because they are
concerned with different constraints -- more generally, with
different "figures of merit."

But I got 32 as the answer , using sizeof .

... which was the correct answer; bravo! (Of course, a
different compiler might have given a different answer, which
would *also* have been the correct answer; bravo!)

And when I added
"float f" to the structure , I was getting the same answer , 32 .

Another right answer! You''re on a roll.

Well , if 32 is right instead of 28 in the first case , then after
adding a float , shouldn''t it become 36 ? But even in case 2 , its
32 . Is there more to structure padding than simple clubbing together
in groups of 4 ? Somebody please shed some light ?

Hypothesis: On the system you are using at the moment, the
compiler tries to ensure that every double begins at a memory
address that is (if considered as a number) divisible by eight.
If the hypothesis is correct, does it imply anything about the
alignment requirement for a struct that has a double as one of
its members? Further, does the struct''s alignment requirement
have any influence on its sizeof? Hint: In `struct s array[2];''
ponder the addresses `&array[0]'' and `&array[1]''.

--
Eric Sosman
es*****@ieee-dot-org.invalid


Kislay wrote:

>
case 1 :
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
};

case 2:
struct s
{
char c1[6]; // 8
double d; // 8
int i1; // 4
char c2[2]; // 4
int i2; // 4
float f // 4
};

According to the rules of structure padding shouldn''t the size of the
above structure be 28 bytes (for a 32 bit compiler , I am using DEV-C+
+) ? But I got 32 as the answer , using sizeof . And when I added
"float f" to the structure , I was getting the same answer , 32 .
Well , if 32 is right instead of 28 in the first case , then after
adding a float , shouldn''t it become 36 ? But even in case 2 , its
32 . Is there more to structure padding than simple clubbing together
in groups of 4 ? Somebody please shed some light ?

It looks as if your doubles need to be aligned to an address that
is zero modulo 8. Now explain how an array of two case 1 structs
could do that if the structure size is 28? I.e. they need padding
by an additional 4 bytes. Your float f added just uses that
padding space.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com


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

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