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.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章