StructLayoutAttribute.Pack做了什么!?? [英] what does StructLayoutAttribute.Pack do!??

查看:101
本文介绍了StructLayoutAttribute.Pack做了什么!??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我知道StructLayoutAttribute的作用。我已经看到它被使用了。

但是Pack领域做了什么!?


这就是MSDN所说的:

控制

内存中类或结构的数据字段的对齐。

此字段指示
$ b时应使用的包装大小$ b指定LayoutKind.Sequential值。 Pack的值必须为0,

1,2,4,8,16,32,64或128.值为0表示

打包对齐设置为当前平台的默认值。

我不明白!我搜索了这个论坛和谷歌。什么都没有,

有人可以用一个例子解释一下!?


谢谢


Gideon

hi ,

I'' know what the StructLayoutAttribute does. I''ve seen it being used.
But what does the Pack field do!?

This is what MSDN says:
Controls the alignment of data fields of a class or structure in
memory.
This field indicates the packing size that should be used when the
LayoutKind.Sequential value is specified. The value of Pack must be 0,
1, 2, 4, 8, 16, 32, 64, or 128. A value of 0 indicates that the
packing alignment is set to the default for the current platform.
I dont get it! I''ve searched this forum and google. Got nothing ,
could someone please explain it with perhaps an example!?

Thanks

Gideon

推荐答案

2007年5月29日星期二12:25:00 -0700,giddy< gi ******* @ gmail .comwrote:
On Tue, 29 May 2007 12:25:00 -0700, giddy <gi*******@gmail.comwrote:

我知道StructLayoutAttribute的作用。我已经看到它被使用了。

但是Pack领域做了什么!?
I'' know what the StructLayoutAttribute does. I''ve seen it being used.
But what does the Pack field do!?



包装指的是结构中的字段如何对齐,以
字节表示。如果打包只有1个字节,则

字段之间没有间隙。但是在更高的包装值时,每个字段都对齐

确保它相对于

结构的开头的偏移量开始,该结构是给定的倍数包装价值。如果给定字段

在一个字段之后,该字段本身不是

大小的包装数的倍数,那么将插入足够的字节以确保给定字段

以字节偏移开始,*是*包装数的倍数。


例如,考虑这种结构:


struct A

{

byte b1;

byte b2;

}


字段b1将始终位于偏移0.但是字段b2可能处于各种偏移量,具体取决于打包。如果打包为1,那么

b2将在字段b1之后的偏移1处。但是如果包装是(例如

)4,则b2将在偏移4处。


如果在b2之前在上述结构中有更多字段,然后b2将

结束不同的抵消,再次取决于包装价值。

但是,总是,是包装价值的倍数。

Pete

"Packing" refers to how fields within a structure are aligned, in terms of
bytes. If the packing is just 1 byte, then there are no gaps between
fields. But at higher values for the packing, each field is aligned to
make sure it starts on an offset relative to the beginning of the
structure that is a multiple of the given packing value. If a given field
is after a field that is not itself a multiple of the packing number in
size, then enough bytes will be inserted to make sure that given field
starts at an byte offset that *is* a multiple of the packing number.

For example, consider this structure:

struct A
{
byte b1;
byte b2;
}

The field b1 will always be at offset 0. But the field b2 could be at a
variety of offsets, depending on the packing. If the packing is 1, then
b2 will be at offset 1, right after field b1. But if packing is (for
example) 4, the b2 will be at offset 4.

If you had more fields in the above structure before b2, then b2 would
wind up at different offsets, again depending on the packing value.
Always, however, a multiple of the packing value.

Pete


struct A
struct A

{

byte b1 ;

字节b2;

}
{
byte b1;
byte b2;
}



错误示例,b2应始终位于偏移量1,因为a在打包时,相同类型的
变量序列被一起处理。


而是考虑:


struct A

{

byte b1;

double b2;

}


以下所有评论都是正确的。

Bad example, b2 should always be at offset 1, because a sequence of
variables of the same type get treated together when packing.

Instead consider:

struct A
{
byte b1;
double b2;
}

And all the following comments are then correct.


>

字段b1将始终位于偏移0处。但是字段b2可能是根据包装情况,各种补偿价格为
。如果打包为1,那么

b2将在字段b1之后的偏移1处。但是如果包装是(例如

)4,则b2将在偏移4处。


如果在b2之前在上述结构中有更多字段,然后b2将

结束不同的抵消,再次取决于包装价值。

但是,总是,是包装价值的倍数。

Pete
>
The field b1 will always be at offset 0. But the field b2 could be at a
variety of offsets, depending on the packing. If the packing is 1, then
b2 will be at offset 1, right after field b1. But if packing is (for
example) 4, the b2 will be at offset 4.

If you had more fields in the above structure before b2, then b2 would
wind up at different offsets, again depending on the packing value.
Always, however, a multiple of the packing value.

Pete



" giddy" < gi ******* @ gmail.com写信息

news:11 ******************** @ x35g2000prf。 googlegrou ps.com ...
"giddy" <gi*******@gmail.comwrote in message
news:11********************@x35g2000prf.googlegrou ps.com...

hi,


我知道StructLayoutAttribute的作用。我已经看到它被使用了。

但是Pack领域做了什么!?


这就是MSDN所说的:

控制

内存中类或结构的数据字段的对齐。

此字段指示
$ b时应使用的包装大小$ b指定LayoutKind.Sequential值。 Pack的值必须为0,

1,2,4,8,16,32,64或128.值为0表示

打包对齐设置为当前平台的默认值。


我不明白!我搜索了这个论坛和谷歌。什么都没有,

有人可以用一个例子解释一下!?


谢谢


Gideon
hi ,

I'' know what the StructLayoutAttribute does. I''ve seen it being used.
But what does the Pack field do!?

This is what MSDN says:
Controls the alignment of data fields of a class or structure in
memory.
This field indicates the packing size that should be used when the
LayoutKind.Sequential value is specified. The value of Pack must be 0,
1, 2, 4, 8, 16, 32, 64, or 128. A value of 0 indicates that the
packing alignment is set to the default for the current platform.
I dont get it! I''ve searched this forum and google. Got nothing ,
could someone please explain it with perhaps an example!?

Thanks

Gideon



在其他人说的最重要的一些评论中:

- Pack 8的任何值默认为8.

- 内存中对象/结构的大小,将是Pack

值的倍数,除非对象/结构的大小小于Pack

的值在哪种情况下,它的大小是4(sizeof(int))。


以下是一些说明上述内容的示例。

1)

[StructLayout(LayoutKind.Sequential,Pack = 8)]

struct S

{

byte b1;

byte b2;

}


struct = 4的大小(此处在堆栈中)。

size当编组= 2.


2)

[StructLayout(LayoutKind.Sequential,Pack = 8)]

struct S

{

字节b1; // + 0

字节b2; // +1

长l1; // + 8

}


struct的大小= 16(在堆栈上)
当封送= 16时,
大小。


3)

[StructLayout(LayoutKind.Sequential,Pack = 8)]

struct S

{

byte b1; // + 0

字节b2; // +1

长l1; // + 8

int i1; // + 16

}


struct = 24的大小(在堆栈上)
当编组= 24时,
大小。


4)

[StructLayout(LayoutKind.Sequential,Pack = 4)]

struct S

{

byte b1; // + 0

字节b2; // +1

长l1; // +4

int i1; // + 12

字节b3; // + 16

}


struct的大小= 20(在堆栈上)
当封送= 20时
大小。


威利。

PS。表示相同数据的堆上对象的大小是struct + 8的大小(或者对于64位版本的应用程序为+ 16)


威利。


In top what others said, some remarks:
- any value of Pack 8 defaults to 8.
- the size of the object/structure in memory, will be a multiple of the Pack
value, unless the size of the object/structure is smaller than the Pack
value in which case it''s size is 4 (sizeof(int)).

Following are some samples illustrating the above.
1)
[StructLayout(LayoutKind.Sequential, Pack=8)]
struct S
{
byte b1;
byte b2;
}

size of the struct = 4 (here on the stack).
size when marshaling = 2.

2)
[StructLayout(LayoutKind.Sequential, Pack=8)]
struct S
{
byte b1; // +0
byte b2; // +1
long l1; // +8
}

size of the struct = 16 (on the stack)
size when marshaling = 16.

3)
[StructLayout(LayoutKind.Sequential, Pack=8)]
struct S
{
byte b1; // +0
byte b2; // +1
long l1; // +8
int i1; // +16
}

size of the struct = 24 (on the stack)
size when marshaling = 24.

4)
[StructLayout(LayoutKind.Sequential, Pack=4)]
struct S
{
byte b1; // +0
byte b2; // +1
long l1; // +4
int i1; // +12
byte b3; // +16
}

size of the struct = 20 (on the stack)
size when marshaling = 20.

Willy.
PS. The size of the objects on the heap representing the same data is the
size of the struct + 8 (or + 16 , for 64 bit versions of the application)

Willy.


这篇关于StructLayoutAttribute.Pack做了什么!??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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