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

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

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

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

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

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

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

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

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

       
       
[csharp] view plain copy print ?
  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. unsafe static void Main()
  12. {
  13. int i = 5;
  14. // Unsafe method: uses address-of operator (&):
  15. SquarePtrParam(&i);
  16. Console.WriteLine(i);
  17. }
  18. }
输出
25
 
 
fixed 语句禁止垃圾回收器重定位可移动的变量。fixed 语句只能出现在不安全的上下文中。Fixed 还可用于创建固定大小的缓冲区
   
   

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

         
         
[csharp] view plain copy print ?
  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 print ?
  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 print ?
  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. }
执行完语句中的代码后,任何固定变量都被解除固定并受垃圾回收的制约。因此,不要指向 fixed 语句之外的那些变量。
Note注意

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

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值