中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

C# 不安全代碼

當(dāng)一個(gè)代碼塊使用 unsafe 修飾符標(biāo)記時(shí),C# 允許在函數(shù)中使用指針變量。不安全代碼或非托管代碼是指使用了指針變量的代碼塊。

指針變量

指針 是值為另一個(gè)變量的地址的變量,即,內(nèi)存位置的直接地址。就像其他變量或常量,您必須在使用指針存儲(chǔ)其他變量地址之前聲明指針。

指針變量聲明的一般形式為:

type* var-name;

下面是指針類型聲明的實(shí)例:

實(shí)例 描述
int* p p 是指向整數(shù)的指針。
double* p p 是指向雙精度數(shù)的指針。
float* p p 是指向浮點(diǎn)數(shù)的指針。
int** p p 是指向整數(shù)的指針的指針。
int*[] p p 是指向整數(shù)的指針的一維數(shù)組。
char* p p 是指向字符的指針。
void* p p 是指向未知類型的指針。

在同一個(gè)聲明中聲明多個(gè)指針時(shí),星號(hào) * 僅與基礎(chǔ)類型一起寫入;而不是用作每個(gè)指針名稱的前綴。 例如:

int* p1, p2, p3;     // 正確  
int *p1, *p2, *p3;   // 錯(cuò)誤 

下面的實(shí)例說(shuō)明了 C# 中使用了 unsafe 修飾符時(shí)指針的使用:

實(shí)例

using System;
namespace UnsafeCodeApplication
{
? ? class Program
? ? {
? ? ? ? static unsafe void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? int var = 20;
? ? ? ? ? ? int* p = &var;
? ? ? ? ? ? Console.WriteLine("Data is: {0} ", ?var);
? ? ? ? ? ? Console.WriteLine("Address is: {0}", ?(int)p);
? ? ? ? ? ? Console.ReadKey();
? ? ? ? }
? ? }
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

Data is: 20
Address is: 99215364

您也可以不用聲明整個(gè)方法作為不安全代碼,只需要聲明方法的一部分作為不安全代碼。下面的實(shí)例說(shuō)明了這點(diǎn)。

使用指針檢索數(shù)據(jù)值

您可以使用 ToString() 方法檢索存儲(chǔ)在指針變量所引用位置的數(shù)據(jù)。下面的實(shí)例演示了這點(diǎn):

實(shí)例

using System;
namespace UnsafeCodeApplication
{
? ?class Program
? ?{
? ? ? public static void Main()
? ? ? {
? ? ? ? ?unsafe
? ? ? ? ?{
? ? ? ? ? ? int var = 20;
? ? ? ? ? ? int* p = &var;
? ? ? ? ? ? Console.WriteLine("Data is: {0} " , var);
? ? ? ? ? ? Console.WriteLine("Data is: {0} " , p->ToString());
? ? ? ? ? ? Console.WriteLine("Address is: {0} " , (int)p);
? ? ? ? ?}
? ? ? ? ?Console.ReadKey();
? ? ? }
? ?}
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

Data is: 20
Data is: 20
Address is: 77128984

傳遞指針作為方法的參數(shù)

您可以向方法傳遞指針變量作為方法的參數(shù)。下面的實(shí)例說(shuō)明了這點(diǎn):

實(shí)例

using System;
namespace UnsafeCodeApplication
{
? ?class TestPointer
? ?{
? ? ? public unsafe void swap(int* p, int *q)
? ? ? {
? ? ? ? ?int temp = *p;
? ? ? ? ?*p = *q;
? ? ? ? ?*q = temp;
? ? ? }

? ? ? public unsafe static void Main()
? ? ? {
? ? ? ? ?TestPointer p = new TestPointer();
? ? ? ? ?int var1 = 10;
? ? ? ? ?int var2 = 20;
? ? ? ? ?int* x = &var1;
? ? ? ? ?int* y = &var2;
? ? ? ? ?
? ? ? ? ?Console.WriteLine("Before Swap: var1:{0}, var2: {1}", var1, var2);
? ? ? ? ?p.swap(x, y);

? ? ? ? ?Console.WriteLine("After Swap: var1:{0}, var2: {1}", var1, var2);
? ? ? ? ?Console.ReadKey();
? ? ? }
? ?}
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

Before Swap: var1: 10, var2: 20
After Swap: var1: 20, var2: 10

使用指針訪問(wèn)數(shù)組元素

在 C# 中,數(shù)組名稱和一個(gè)指向與數(shù)組數(shù)據(jù)具有相同數(shù)據(jù)類型的指針是不同的變量類型。例如,int *p 和 int[] p 是不同的類型。您可以增加指針變量 p,因?yàn)樗趦?nèi)存中不是固定的,但是數(shù)組地址在內(nèi)存中是固定的,所以您不能增加數(shù)組 p。

因此,如果您需要使用指針變量訪問(wèn)數(shù)組數(shù)據(jù),可以像我們通常在 C 或 C++ 中所做的那樣,使用 fixed 關(guān)鍵字來(lái)固定指針。

下面的實(shí)例演示了這點(diǎn):

實(shí)例

using System;
namespace UnsafeCodeApplication
{
? ?class TestPointer
? ?{
? ? ? public unsafe static void Main()
? ? ? {
? ? ? ? ?int[] ?list = {10, 100, 200};
? ? ? ? ?fixed(int *ptr = list)

? ? ? ? ?/* 顯示指針中數(shù)組地址 */
? ? ? ? ?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.ReadKey();
? ? ? }
? ?}
}

當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

Address of list[0] = 31627168
Value of list[0] = 10
Address of list[1] = 31627172
Value of list[1] = 100
Address of list[2] = 31627176
Value of list[2] = 200

編譯不安全代碼

為了編譯不安全代碼,您必須切換到命令行編譯器指定 /unsafe 命令行。

例如,為了編譯包含不安全代碼的名為 prog1.cs 的程序,需在命令行中輸入命令:

csc /unsafe prog1.cs

如果您使用的是 Visual Studio IDE,那么您需要在項(xiàng)目屬性中啟用不安全代碼。

步驟如下:

  • 通過(guò)雙擊資源管理器(Solution Explorer)中的屬性(properties)節(jié)點(diǎn),打開(kāi)項(xiàng)目屬性(project properties)
  • 點(diǎn)擊 Build 標(biāo)簽頁(yè)。
  • 選擇選項(xiàng)"Allow unsafe code"。