在结构内创建数组时,该结构是否直接包含数组的值或对该数组的内存引用? [英] While Creating a Array Inside a Structure , Does that Structure Contain Directly the Value of Array or the Memory reference to that array?

查看:41
本文介绍了在结构内创建数组时,该结构是否直接包含数组的值或对该数组的内存引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个数组,如下所示:

I am creating an array as below:

public struct Smb_Parameters
        {
            public byte WordCount;
            public ushort[] Words;
        }

虽然我分配如下值:

Smb_Parameters smbParameter = new Smb_Parameters();
smbParameter.WordCount = 0;
string words= "NT LM 0.12";
smbParameter.Words = Encoding.ASCII.GetBytes(name);

在上面的赋值中 smbParameter.WordCount 包含值 0smbParameter.Words 是否直接包含值(字节数组)或对包含值的位置的内存引用?

In the above assignment smbParameter.WordCount contains the value 0 but does smbParameter.Words contains directly the values(Arry of byteS) or memory reference to the location that contains the values?

编辑 1:

我想向服务器发送一个数据包.为此,我需要使用以下代码将 Smb_Parameters 对象转换为数组:

I want to send a packet to the server . For that I need to convert Smb_Parameters object to array using the following code:

int len = Marshal.SizeOf(Smb_Parameters);
 byte[] arr = new byte[len];
 IntPtr ptr = Marshal.AllocHGlobal(len);
 Marshal.StructureToPtr(Smb_Parameters, ptr, true);
 Marshal.Copy(ptr, arr, 0, len);
 Marshal.FreeHGlobal(ptr);

推荐答案

除非你使用 fixed-size buffers 在不安全的代码中,这将是一个参考.一般来说,变量是包含在结构体还是类中并不重要——或者实际上它是否是局部变量:变量的类型决定了值是引用还是直接包含数据.除了我提到的固定大小的缓冲区之外,数组在 .NET 中始终是引用类型.

Unless you use fixed-size buffers in unsafe code, it will be a reference. In general, it doesn't matter whether a variable is contained within a struct or a class - or indeed whether it's a local variable: the type of the variable decides whether the value is a reference or whether it contains the data directly. Aside from the fixed-size buffers I've mentioned, arrays are always reference types in .NET.

(还有 stackalloc,它看起来有点像一个数组分配 - 它在堆栈上分配一块内存.同样,这仅用于不安全的代码,在我的经验中很少遇到.)

(There's also stackalloc which looks a bit like an array allocation - it allocates a block of memory on the stack. Again, this is only for use in unsafe code, and very rarely encountered in my experience.)

在您给出的示例中(适当调整以使其编译)Words 绝对可以作为参考.特别是,如果您分配值以引用特定数组,然后更改该数组的内容,则更改将通过结构的变量可见.

In the example you've given (suitably adjusted to make it compile) Words would definitely be a reference. In particular, if you assign the value to refer to a particular array and then change the contents of that array, the changes will be visible via the struct's variable.

(我还强烈建议您不要有公共字段或可变值类型.希望这只是为了举例:)

(I'd also strongly recommend that you don't have public fields or mutable value types. Hopefully this was just for the sake of an example though :)

要回答更新的问题,不要发送那样的数据.这是一种非常脆弱的序列化任何东西的方式.有各种更好的选择:

To answer the updated question, don't send data like that. It's a horribly fragile way of serializing anything. There are various better options:

  • 使用 .NET 二进制序列化(在版本控制方面也有些脆弱,而不是跨平台)
  • 使用 XML 序列化(在这种情况下可能有点矫枉过正)
  • 使用第三方序列化框架(例如 Thrift 或 Protocol Buffers)
  • 在您的类中使用ToByteArray"或WriteToStream"方法手动序列化,显式序列化每个值.为此,您可能需要使用 BinaryWriter.

这篇关于在结构内创建数组时,该结构是否直接包含数组的值或对该数组的内存引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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