C# 不安全代碼

c# 不安全代碼

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

 

1. 指針變量

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

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

type* var-name;

下面是指針類型聲明的范例:

范例 描述
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ò)誤 

下面的范例說(shuō)明了 c# 中使用了 unsafe 修飾符時(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è)方法作為不安全代碼,只需要聲明方法的一部分作為不安全代碼。下面的范例說(shuō)明了這點(diǎn)。

 

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

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

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

 

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

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

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

 

4. 使用指針訪問(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)固定指針。

下面的范例演示了這點(diǎn):

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

 

5. 編譯不安全代碼

為了編譯不安全代碼,您必須切換到命令行編譯器指定 /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"。

下一節(jié):c# 多線程

c# 教程

相關(guān)文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频