简体   繁体   English

包含数组的Struct的大小

[英]Size of a Struct containing an array

I am using ac# struct as a pseudo-union (by using the LayoutKind.Explicit attribute), to pass network messages around my program. 我将ac#struct用作伪联盟(通过使用LayoutKind.Explicit属性),以在程序周围传递网络消息。 I understand how to use the layout with the primitive types, as they are of a know size. 我知道如何将布局与基本类型一起使用,因为它们的大小已知。

However, how would I do this with one of the fields being a char array? 但是,我如何将其中一个字段作为char数组来执行此操作? I know a char is 2 bytes of data (when in unicode format), but how big is char[]? 我知道一个char是2个字节的数据(采用Unicode格式时),但是char []有多大? Am I correct in believing that this is a reference type, so its size is not just number of items * 2? 我是否认为这是引用类型是正确的,所以它的大小不仅仅是项目数* 2?

How would I layout the struct for this? 我将如何为此布局结构? Is it even possible? 可能吗?

The size is the width of a reference; 大小是参考的宽度; so 4 bytes on x86 or 8 bytes on x64. 因此,x86上为4个字节,x64上为8个字节。 The size of the array is irrelevant, as the array is stored separately on the heap. 数组的大小无关紧要,因为该数组分别存储在堆中。 If you want to serialize that data to a byte stream, then it probably depends on which encoding you use for the char data. 如果要将该数据序列化为字节流,则可能取决于用于char数据的编码 UTF16 would indeed be 2 * number of characters, but UTF8 or UTF32 will be different. UTF16的确是2 *个字符,但是UTF8或UTF32会有所不同。

奇怪,不是等于长度乘以每个字符的字节数吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM