简体   繁体   English

从内存中清除字符

[英]Clearing a char from memory

While i can easily get a pointer of a char from a string: 虽然我可以轻松地从字符串中获取char的指针:

public static unsafe void Clear(this string s)
{
        fixed (char* charPtr = s)
        {
            for (int i = 0; i < s.Length; i++)
                charPtr[i] = '\0';
        }
}

I can't seem to do the same for char: fixed (char* c = ch) does not work and gives me following error: 我似乎无法对char做同样的事情: fixed (char* c = ch)不起作用,并给我以下错误:

You cannot use the fixed statement to take the adress of an already fixed expression. 您不能使用固定语句来获取已经固定的表达式的地址。 Cannot implicitly convert type 'char' to 'char*' 无法将类型'char'隐式转换为'char *'

Is there anyway in C# that I can reach the pointer of the char and delete it (= '\\0')? 无论如何,在C#中,我可以到达char的指针并将其删除(='\\ 0')?

The char type is already mutable. char类型已经可变。 So you can simply set its value to 0. 因此,您只需将其值设置为0。

Assume you have the following char : 假设您具有以下char

char c = 'a';

Simply set it to 0 like this: 只需将其设置为0即可,如下所示:

c = '\0';

I am assuming here that you have direct access to the field/variable. 我在这里假设您可以直接访问该字段/变量。 Note that if you receive a char as a method argument or if you get it by invoking a property, then you get a copy of the original char . 请注意,如果您将char作为方法参数接收,或者通过调用属性来获取它,那么您将获得原始char的副本。 If you change that to 0 then you are mutating the copy of the original char . 如果将其更改为0,那么您正在变异原始char的副本。

Since characters are passed by value, you would need to pass the character argument in by reference: 由于字符是通过值传递的,因此您需要通过引用传递character参数:

public static unsafe void Clear(ref char s)
{
    fixed (char* n = &s)
    {
        *n = '\0';
    }
}

In this case, unsafe code is unnecessary; 在这种情况下,不需要安全代码。 the above can be rewritten as simply: 以上内容可以简单地重写为:

public static void Clear(ref char s)
{
     s = '\0';
}

Note that since the first parameter of an extension method cannot have a ref modifier, the above cannot be used as an extension method. 请注意,由于扩展方法的第一个参数不能具有ref修饰符,因此上述内容不能用作扩展方法。


Separately, your Clear(string) method relies on undefined behavior. 另外,您的Clear(string)方法依赖于未定义的行为。 While unsafe semantics do allow to mutate a string, this can cause unexpected results. 尽管不安全的语义确实允许更改字符串,但这可能会导致意外的结果。 Per the language specification (emphasis added): 根据语言规范(添加了重点):

Modifying objects of managed type through fixed pointers can results in undefined behavior. 通过固定指针修改托管类型的对象可能导致未定义的行为。 For example, because strings are immutable, it is the programmer's responsibility to ensure that the characters referenced by a pointer to a fixed string are not modified. 例如,由于字符串是不可变的, 因此程序员有责任确保不修改指向固定字符串的指针所引用的字符。

To illustrate the undefined behavior, consider the following code: 为了说明未定义的行为,请考虑以下代码:

string d = "E";
string f = "E";
d.Clear();
Console.WriteLine(d);
Console.WriteLine(f);
Console.WriteLine("E");

Because of string interning, the all three strings are stored in the same memory location. 由于字符串插入,所有这三个字符串都存储在同一存储位置。 As a result, the code above prints three blank lines. 结果,上面的代码打印了三个空白行。

If the motivation for clearing the string is to clear sensitive data from memory, a supported approach is to use SecureString . 如果清除字符串的动机是从内存中清除敏感数据,则支持的方法是使用SecureString Since the GC can relocate memory as needed, there is no assurance that clearing the variable's current address will remove all references to the string from memory. 由于GC可以根据需要重定位内存,因此无法保证清除变量的当前地址会从内存中删除对该字符串的所有引用。

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

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