Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有指针的不安全C#代码段_C#_Pointers - Fatal编程技术网

带有指针的不安全C#代码段

带有指针的不安全C#代码段,c#,pointers,C#,Pointers,我遇到了以下代码片段,需要预测输出。我的答案是220,但被告知是错的。有人能告诉我正确的输出,并请解释原因 using System; class pointer { public static void Main() { int ptr1=0; int* ptr2=&ptr1; *ptr2=220; Console.WriteLine(ptr1); } } 编辑: 谢谢大家的解释性回答。当且仅当上述代码块(即C代码,抱歉在问题中未提及)被声明为非托

我遇到了以下代码片段,需要预测输出。我的答案是220,但被告知是错的。有人能告诉我正确的输出,并请解释原因

using System;
class pointer
{
  public static void Main()
  {
   int ptr1=0;
   int* ptr2=&ptr1;
   *ptr2=220;
   Console.WriteLine(ptr1);
  }
}
编辑:
谢谢大家的解释性回答。当且仅当上述代码块(即C代码,抱歉在问题中未提及)被声明为非托管时,正确答案肯定是220。谢谢你的回答。em中的每一个都提供了丰富的信息和帮助。

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

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

但是,如果您写下以下内容:

int ptr1 = 0;

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

Console.WriteLine(ptr1);
那你真的会得到220分

您还可以创建整个不安全方法,而不是创建特定的不安全块:

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


<> > >编辑< /强>:查看一个简短、有趣但信息丰富的指针指针。

< P>结果是220,下面是一个C代码代码片段,用于测试它(这里没有C++)

步骤如下:

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

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

我对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);
}

@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#中使用过指针(从未使用过)


我做了一个快速查找,它帮助我生成了上述示例。

这是作为不安全代码处理的不安全代码吗?在C/C++中,它应该是220…220,祝你在剩下的家庭作业中好运,它的C#带有不安全的代码。但是,代码应该被包装在一个不安全的块中。当你回答220并把它弄错时,显然你想要的答案是它不构建b/c。你不是在一个不安全的环境中,c允许这样使用指针。+1对于很棒的变量命名,ptr1不是指针。没问题:)我学到了一些新东西,所以我很高兴与大家分享。希望有人能评估一下。我的代码有一点,让我知道他们对它的看法。:)第3行应该是“PTR2点处的整数被赋值为220”。PTR2的值(PTR1的内存位置)没有更改。@Zyphrax:我将您的“步骤”部分复制到一些文档中供以后使用。谢谢。:)
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);
    }
}