C#中指针*的使用(unsafe关键字与fixed 语句)

229 篇文章 2 订阅
unsafe 关键字表示不安全上下文,该上下文是任何涉及指针的操作所必需的。有关更多信息,请参见 不安全代码和指针(C# 编程指南)

可以在类型或成员的声明中使用 unsafe 修饰符。因此,类型或成员的整个正文范围均被视为不安全上下文。例如,以下是用 unsafe 修饰符声明的方法:

 
     
     
[csharp] view plain copy
  1. unsafe static void FastCopy(byte[] src, byte[] dst, int count)  
  2. {  
  3.     // Unsafe context: can use pointers here.  
  4. }  

不安全上下文的范围从参数列表扩展到方法的结尾,因此指针在以下参数列表中也可以使用:

 

     
     
[csharp] view plain copy
  1. unsafe static void FastCopy ( byte* ps, byte* pd, int count ) {...}  

还可以使用不安全块从而能够使用该块内的不安全代码。例如:

 
     
     
[csharp] view plain copy
  1. unsafe  
  2. {  
  3.     // Unsafe context: can use pointers here.  
  4. }  

若要编译不安全代码,必须指定 /unsafe 编译器选项。无法通过公共语言运行库验证不安全代码。

 
       
       
[csharp] view plain copy
  1. // cs_unsafe_keyword.cs  
  2. // compile with: /unsafe  
  3. using System;  
  4. class UnsafeTest  
  5. {  
  6.    // Unsafe method: takes pointer to int:  
  7.    unsafe static void SquarePtrParam(int* p)  
  8.    {  
  9.       *p *= *p;  
  10.    }  
  11.   
  12.    unsafe static void Main()  
  13.    {  
  14.       int i = 5;  
  15.       // Unsafe method: uses address-of operator (&):  
  16.       SquarePtrParam(&i);  
  17.       Console.WriteLine(i);  
  18.    }  
  19. }  
输出
25
 
 
fixed 语句禁止垃圾回收器重定位可移动的变量。fixed 语句只能出现在不安全的上下文中。Fixed 还可用于创建固定大小的缓冲区 
    
    

fixed 语句设置指向托管变量的指针并在 statement 执行期间“钉住”该变量。如果没有 fixed 语句,则指向可移动托管变量的指针的作用很小,因为垃圾回收可能不可预知地重定位变量。C# 编译器只允许在 fixed 语句中分配指向托管变量的指针。

         
         
[csharp] view plain copy
  1. // assume class Point { public int x, y; }  
  2. // pt is a managed variable, subject to garbage collection.  
  3. Point pt = new Point();  
  4. // Using fixed allows the address of pt members to be  
  5. // taken, and "pins" pt so it isn't relocated.  
  6. fixed ( int* p = &pt.x )  
  7. {  
  8.     *p = 1;   
  9. }  

可以用数组或字符串的地址初始化指针:

         
         
[csharp] view plain copy
  1. fixed (int* p = arr) ...  // equivalent to p = &arr[0]  
  2. fixed (char* p = str) ... // equivalent to p = &str[0]  

只要指针的类型相同,就可以初始化多个指针:

fixed (byte* ps = srcarray, pd = dstarray) {...}

要初始化不同类型的指针,只需嵌套 fixed 语句:

[csharp] view plain copy
  1. <pre class="csharp" name="code">fixed (int* p1 = &p.x)  
  2. {  
  3.     fixed (double* p2 = &array[5])  
  4.     {  
  5.         // Do something with p1 and p2.  
  6.     }  
  7. }  
  8. </pre>执行完语句中的代码后,任何固定变量都被解除固定并受垃圾回收的制约。因此,不要指向 <strong>fixed</strong> 语句之外的那些变量。  
Note注意

无法修改在 fixed 语句中初始化的指针。

在不安全模式中,可以在堆栈上分配内存。堆栈不受垃圾回收的制约,因此不需要被锁定。有关更多信息,请参见 stackalloc

 
 
  
  
  
  
[csharp] view plain copy
  1. // statements_fixed.cs  
  2. // compile with: /unsafe  
  3. using System;  
  4.   
  5. class Point  
  6. {   
  7.     public int x, y;   
  8. }  
  9.   
  10. class FixedTest   
  11. {  
  12.     // Unsafe method: takes a pointer to an int.  
  13.     unsafe static void SquarePtrParam (int* p)   
  14.     {  
  15.         *p *= *p;  
  16.     }  
  17.   
  18.     unsafe static void Main()   
  19.     {  
  20.         Point pt = new Point();  
  21.         pt.x = 5;  
  22.         pt.y = 6;  
  23.         // Pin pt in place:  
  24.         fixed (int* p = &pt.x)   
  25.         {  
  26.             SquarePtrParam (p);  
  27.         }  
  28.         // pt now unpinned  
  29.         Console.WriteLine ("{0} {1}", pt.x, pt.y);  
  30.     }  
  31. }  
输出
25 6
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值