简体   繁体   English

从程序中删除 byte[] 和 C# 中的 memory

[英]Deleting byte[] from program and memory in C#

I'm making a program in which one of its functions, in order to correctly create the message to be sent, keeps calling a function I have generated to add each of the parts to the array.我正在制作一个程序,其中一个函数为了正确创建要发送的消息,不断调用我生成的 function 以将每个部分添加到数组中。 The thing is, in C# you can't do this because the byte arrays (and if I'm not wrong, any kind of array) has a finite Length which cannot be changed.问题是,在 C# 中你不能这样做,因为字节 arrays (如果我没记错的话,任何类型的数组)都有一个无法更改的有限长度。

Due to this, I thought of creating 2 byte variables.因此,我想到了创建 2 字节变量。 The first one would get the first to values.第一个将获得第一个值。 The second one would be created after you know the quantity of new bytes you have to add, and after this, you would delete the first variable and create it again, with the Length of the previous variable, but adding the Length of the new values, doing the same you did with the second variable.第二个将在您知道必须添加的新字节数量后创建,然后,您将删除第一个变量并再次创建它,使用前一个变量的长度,但添加新值的长度,对第二个变量做同样的事情。 The code I've generated is:我生成的代码是:

                byte[] message_mod_0 = adr_and_func;
                byte[] byte_memory_adr = AddAndTypes.ToByteArray(memory_adr);
                byte[] message_mod_1 = new byte[2 + byte_memory_adr.Length];
                message_mod_1 = AddAndTypes.AddByteArrayToByteArray(message_mod_0, byte_memory_adr);
                AddAndTypes.AddByteArrayToByteArray(message_mod_0, AddAndTypes.IntToByte(value));
                byte[] CRC = Aux.CRC(message_mod_0);
                AddAndTypes.AddByteArrayToByteArray(message_mod_0, CRC);

In this code, the two variables I've meant are message_mod_0 and message_mod_1.在这段代码中,我所指的两个变量是 message_mod_0 和 message_mod_1。 I also think of doing the deleting and redeclaring the byte_memory_adr variable that is required in order to know which is the Length of the byte array you want to add to the ouput message.我还考虑执行删除和重新声明所需的 byte_memory_adr 变量,以便知道要添加到输出消息中的字节数组的长度。

The parameters adr_and_func, memory_adr and value are given as input parameters of the function I'm making.参数 adr_and_func、memory_adr 和 value 作为我正在制作的 function 的输入参数给出。

The question can be summed up as: is there any way to delete variables in the same scope they were created?问题可以总结为:有什么方法可以删除它们创建的同一个 scope 中的变量? And, in case it can be done, would there be any problem if I created a new variable with the same name after I have deleted the first one?而且,如果可以做到,如果我在删除第一个变量后创建一个同名的新变量,会有什么问题吗? I can't think of any reason why that could happen, but I'm pretty new to this programming language.我想不出为什么会发生这种情况,但我对这种编程语言还是很陌生。

Also, I don't know if there is any less messy way of doing this.另外,我不知道是否有任何不那么混乱的方式来做到这一点。

This sounds like you are writing your own custom serializer.这听起来就像您正在编写自己的自定义序列化程序。

I would recommend just using a existing library, like protobuf.net to define your messages if at all possible.如果可能的话,我建议只使用现有的库,比如protobuf.net来定义你的消息。

If this is not possible you can use a BinaryWriter to write your values to a Stream .如果这是不可能的,您可以使用BinaryWriter将您的值写入Stream If you want to keep it in memory use a MemoryStream and use.ToArray() when your done to get a array of all bytes.如果您想将其保留在 memory 中,请使用MemoryStream并在完成后使用.ToArray() 来获取所有字节的数组。

As for memory, do not worry about it.至于memory,不用担心。 Unless you have gigabyte sized messages the memory requirements should not be an issue, and the garbage collector will automatically recycle memory when it is no longer needed, and it can do this after the last usage, regardless of scope.除非您有千兆字节大小的消息,否则 memory 要求应该不是问题,并且垃圾收集器将在不再需要时自动回收 memory,并且它可以在最后一次使用后执行此操作,而不管 Z31A1FD140BE4BEF2D8ZA121EC9A155A8 If you have huge memory streams you might want to look at something like recyclable memory stream since this can avoid some allocation performance issues and fragmentation issues.如果你有巨大的 memory 流,你可能想看看像可回收的 memory stream这样的东西,因为这可以避免一些分配性能问题和碎片问题。

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

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