C# 不安全代码:fixed关键字

C# 不安全代码:fixed关键字


当一个代码块使用 unsafe 修饰符标记时,C# 允许在函数中使用指针变量。不安全代码或非托管代码是指使用了指针变量的代码块。

编译不安全代码

为了编译不安全代码,您必须切换到命令行编译器指定 /unsafe 命令行。
例如,为了编译包含不安全代码的名为 prog1.cs 的程序,需在命令行中输入命令:

csc /unsafe prog1.cs

如果您使用的是 Visual Studio IDE,那么您需要在项目属性中启用不安全代码。
步骤如下:

  • 通过双击资源管理器(Solution Explorer)中的属性(properties)节点,打开项目属性(project properties)。
  • 点击 Build 标签页。
  • 选择选项"Allow unsafe code"。

fixed关键字

由于C#中声明的变量在内存中的存储受垃圾回收器管理;因此一个变量(例如一个大数组)有可能在运行过程中被移动到内存中的其他位置。如果一个变量的内存地址会变化,那么指针也就没有意义了。

解决方法就是使用fixed关键字来固定变量位置不移动。

unsafe static void Main(string[] args)
{
	int[] list = { 10, 100, 200 };
	fixed (int* ptr = list)
	{
		for (int i = 0; i < 3; i++)
		{
			Console.WriteLine("Address of list[{0}]={1}", i, (int)(ptr + i));
			Console.WriteLine("Value of list[{0}]={1}", i, *(ptr + i));
		}
	}
	Console.ReadLine();
}

结果如下

Address of list[0]=-651547208
Value of list[0]=10
Address of list[1]=-651547204
Value of list[1]=100
Address of list[2]=-651547200
Value of list[2]=200

在unsafe不安全环境中,我们也可以通过stackalloc在堆栈上分配内存,因为在堆栈上分配的内存不受内存管理器管理,因此其相应的指针不需要固定。

unsafe static void Main(string[] args)
{
    const int arraySize = 20;
    int* fib = stackalloc int[arraySize];
    int* p = fib;

    // The sequence begins with 1, 1.
	*p++ = *p++ = 1;
	for (int i = 2; i < arraySize; ++i, ++p)
	{
		// Sum the previous two numbers.
		*p = p[-1] + p[-2];
	}
	for (int i = 0; i < arraySize; ++i)
	{
		Console.WriteLine(fib[i]);
	}

	// Keep the console window open in debug mode.
	System.Console.WriteLine("Press any key to exit.");
	System.Console.ReadKey();
}

结果如下

1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
Press any key to exit.
发布了8 篇原创文章 · 获赞 4 · 访问量 2989

猜你喜欢

转载自blog.csdn.net/yezuofenglin/article/details/100077348