【问题标题】:Unsafe C# code snippet with pointers带有指针的不安全 C# 代码片段
【发布时间】:2009-07-14 19:07:46
【问题描述】:

我遇到了以下代码 sn-p 并需要预测输出。我的回答是 220,但被告知错误。谁能告诉我正确的输出,请解释原因。

using System;
class pointer
{
  public static void Main()
  {
   int ptr1=0;
   int* ptr2=&ptr1;
   *ptr2=220;
   Console.WriteLine(ptr1);
  }
}

编辑: 谢谢大家的解释性答案。当且仅当上述代码块(这是 C# 代码,抱歉在问题中未提及它)被声明为非托管时,正确答案肯定是 220。感谢您的所有回答。他们每个人都非常有用且信息丰富。

【问题讨论】:

  • 这是作为不安全代码处理的不安全吗?在 C/C++ 中应该是 220...
  • 220,祝你功课顺利
  • 这是带有不安全代码的 C#。但它的代码应该被包装在一个不安全的块中。
  • 当您回答 220 并弄错时,显然期望的答案是它不构建 b/c,您不在 C# 允许使用此类指针的不安全上下文中。跨度>
  • +1 用于很棒的变量命名,ptr1 不是指针。

标签: c# pointers


【解决方案1】:

答案是它不能编译。您将收到以下错误:

错误 CS0214:指针和固定大小的缓冲区只能在不安全的上下文中使用

但是,如果你这样写:

int ptr1 = 0;

unsafe {
    int* ptr2 = &ptr1;
    *ptr2 = 220;
}

Console.WriteLine(ptr1);

那么你确实会得到 220。

你也可以创建一个完整的 unsafe 方法,而不是创建特定的 unsafe 块:

public unsafe void Something() {
    /* pointer manipulation */
}

注意:您还必须使用 /unsafe 开关进行编译(在 Visual Studio 的项目属性中选中“允许不安全代码”)

编辑:观看Pointer fun with binky,观看关于指针的简短、有趣但内容丰富的视频。

【讨论】:

    【解决方案2】:

    结果是220,下面是一个C#代码sn-p来测试(这里没有C++)

    using System;
    
    internal class Pointer {
        public unsafe void Main() {
            int ptr1 = 0;
            int* ptr2 = &ptr1;
            *ptr2 = 220;
    
            Console.WriteLine(ptr1);
        }
    }
    

    步骤:

    • PTR1 赋值为 0
    • PTR2 指向 PTR1 的地址空间
    • PTR2 被赋值为 220(但指向 PTR1 的地址空间)
    • 因此,现在请求 PTR1 时,该值也是 220。

    请你的老师也给我一个 A ;)

    【讨论】:

    • 嗯,第 3 行应该是“PTR2 指向的位置处的整数被赋值为 220”。 PTR2 的值(PTR1 的内存位置)没有改变。
    • @Zyphrax:我将您的“步骤”部分复制到一些文档中以供以后使用。谢谢。 :)
    【解决方案3】:

    我对 C# 中的指针一无所知,但我可以尝试解释一下它在 C/C++ 中的作用:

    public static void Main()
    {
      // The variable name is misleading, because it is an integer, not a pointer
      int ptr1 = 0;
    
      // This is a pointer, and it is initialised with the address of ptr1 (@ptr1)
      // so it points to prt1.
      int* ptr2 = &ptr1;
    
      // This assigns to the int variable that ptr2 points to (*ptr2,
      // it now points to ptr1) the value of 220
      *ptr2 = 220;
    
      // Therefore ptr1 is now 220, the following line should write '220' to the console
      Console.WriteLine(ptr1);
    }
    

    【讨论】:

      【解决方案4】:

      @Zaki:您需要标记您的程序集以允许不安全代码并阻止您的不安全代码,如下所示:

      public static class Program {
          [STAThread()]
          public static void Main(params String[] args) {
              Int32 x = 2;
              unsafe {
                  Int32* ptr = &x;
                  Console.WriteLine("pointer: {0}", *ptr);
      
                  *ptr = 400;
                  Console.WriteLine("pointer (new value): {0}", *ptr);
              }
              Console.WriteLine("non-pointer: " + x);
      
              Console.ReadKey(true);
          }
      }
      

      说实话,我从来没有在 C# 中使用过指针(从来没有用过)。

      我快速完成了Google Search 并找到了this,它帮助我生成了上述示例。

      【讨论】:

      • 没问题 :) 我学到了一些新东西,所以很高兴与大家分享。希望有人可以评估。我的代码有点,让我知道他们是怎么想的。 :)
      猜你喜欢
      • 2021-10-17
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      • 2018-11-27
      • 2018-07-27
      • 1970-01-01
      相关资源
      最近更新 更多