简体   繁体   English

在C#中等效于Delphi“ ZeroMemory”?

[英]What is the equivalent of Delphi “ZeroMemory” in C#?

In delphi the "ZeroMemory" procedure, ask for two parameters. 在delphi的“ ZeroMemory”过程中,要求两个参数。

CODE EXAMPLE 代码示例

procedure ZeroMemory(Destination: Pointer; Length: DWORD);
begin
 FillChar(Destination^, Length, 0);
end;

I want make this, or similar in C#... so, what's their equivalent? 我想做到这一点,或在C#中做到类似……所以,它们的等效性是什么?

thanks in advance! 提前致谢!

.NET framework objects are always initialized to a known state .NET框架对象始终被初始化为已知状态

.NET framework value types are automatically 'zeroed' -- which means that the framework guarantees that it is initialized into its natural default value before it returns it to you for use. .NET框架值类型会自动“归零”-这意味着该框架保证在将其返回给您使用之前,将其初始化为自然的默认值。 Things that are made up of value types (eg arrays, structs, objects) have their fields similarly initialized. 由值类型(例如数组,结构,对象)组成的事物的字段也进行了类似的初始化。

In general, in .NET all managed objects are initialized to default, and there is never a case when the contents of an object is unpredictable (because it contains data that just happens to be in that particular memory location) as in other unmanaged environments. 通常,在.NET中,所有托管对象都被初始化为默认值,并且从来没有像其他非托管环境中那样,对象的内容是不可预测的(因为它包含的数据恰好位于该特定内存位置中)。

Answer: you don't need to do this, as .NET will automatically "zero" the object for you. 答:您不需要这样做,因为.NET将自动为您将对象“清零”。 However, you should know what the default value for each value type is. 但是,您应该知道每种值类型的默认值是什么。 For example, the default of a bool is false, and the default of an int is zero. 例如,默认的bool是假的,并且一个的默认int是零。

Unmanaged objects 非托管对象

"Zero-ing" a region of memory is usually only necessary in interoping with external, non-managed libraries. 通常仅在与外部非托管库互操作时才需要“归零”内存区域。

If you have a pinned pointer to a region of memory containing data that you intend to pass to an outside non-managed library (written in C, say), and you want to zero that section of memory, then your pointer most likely points to a byte array and you can use a simple for-loop to zero it. 如果您有一个指向存储区域的固定指针,该存储区域包含要传递给外部非托管库的数据(例如,用C编写),并且您希望将该存储区清零,则该指针很可能指向一个字节数组,您可以使用简单的for循环将其归零。

Off-topic note 离题笔记

On the flip side, if a large object is allocated in .NET, try to reuse it instead of throwing it away and allocating a new one. 另一方面,如果在.NET中分配了一个大对象,请尝试重用它,而不是扔掉它并分配一个新对象。 That's because any new object is automatically "zeroed" by the .NET framework, and for large objects this clearing will cause a hidden performance hit. 这是因为.NET框架会自动将任何新对象“置零”,对于大型对象,这种清除将导致性能下降。

You very rarely need unsafe code in C#. 您很少需要使用C#中的不安全代码。 Usually only when interacting with native libraries. 通常仅在与本机库交互时才使用。

The Marshal class as some low level helper functions, but I'm not aware of any that zeros out memory. 元帅类作为一些低级的辅助函数,但是我不知道有什么会使内存清零的。

Firstly, in .Net (including C#) then value types are zero by default - so this takes away one of the common uses of ZeroMemory. 首先,在.Net(包括C#)中,默认情况下值类型为零-因此,这消除了ZeroMemory的常见用法之一。

Secondly, if you want to zero a list of type T then try a method like: 其次,如果您想将T类型的列表归零,请尝试以下方法:

void ZeroMemory<T>(IList<T> destination)
{
    for (var i=0;i<destination.Count; i+))
   {
       destination[i] = default(T);
   }
}

If a list isn't available... then I think I'd need to see more of the calling code. 如果列表不可用...那么我想我需要查看更多的调用代码。

Technically there is the Array.Clear , but it's only for managed arrays. 从技术上讲,存在Array.Clear ,但仅适用于托管数组。 What do you want to do? 你想让我做什么?

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

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