unsafe 关键字表示不安全上下文,该上下文是任何涉及指针的操作所必需的。有关更多信息,请参见
不安全代码和指针(C# 编程指南)。
可以在类型或成员的声明中使用 unsafe 修饰符。因此,类型或成员的整个正文范围均被视为不安全上下文。例如,以下是用 unsafe 修饰符声明的方法:
unsafe static void FastCopy(byte[] src, byte[] dst, int count) { // Unsafe context: can use pointers here. }
不安全上下文的范围从参数列表扩展到方法的结尾,因此指针在以下参数列表中也可以使用:
unsafe static void FastCopy ( byte* ps, byte* pd, int count ) {...}
还可以使用不安全块从而能够使用该块内的不安全代码。例如:
unsafe { // Unsafe context: can use pointers here. }
若要编译不安全代码,必须指定 /unsafe 编译器选项。无法通过公共语言运行库验证不安全代码。
// cs_unsafe_keyword.cs // compile with: /unsafe using System; class UnsafeTest { // Unsafe method: takes pointer to int: unsafe static void SquarePtrParam(int* p) { *p *= *p; } unsafe static void Main() { int i = 5; // Unsafe method: uses address-of operator (&): SquarePtrParam(&i); Console.WriteLine(i); } }
输出
25
fixed 语句禁止垃圾回收器重定位可移动的变量。fixed 语句只能出现在不安全的上下文中。Fixed 还可用于创建固定大小的缓冲区。
fixed 语句设置指向托管变量的指针并在 statement 执行期间“钉住”该变量。如果没有 fixed 语句,则指向可移动托管变量的指针的作用很小,因为垃圾回收可能不可预知地重定位变量。C# 编译器只允许在 fixed 语句中分配指向托管变量的指针。
// assume class Point { public int x, y; } // pt is a managed variable, subject to garbage collection. Point pt = new Point(); // Using fixed allows the address of pt members to be // taken, and "pins" pt so it isn't relocated. fixed ( int* p = &pt.x ) { *p = 1; }
可以用数组或字符串的地址初始化指针:
fixed (int* p = arr) ... // equivalent to p = &arr[0] fixed (char* p = str) ... // equivalent to p = &str[0]
只要指针的类型相同,就可以初始化多个指针:
fixed (byte* ps = srcarray, pd = dstarray) {...}要初始化不同类型的指针,只需嵌套 fixed 语句:
执行完语句中的代码后,任何固定变量都被解除固定并受垃圾回收的制约。因此,不要指向 fixed 语句之外的那些变量。fixed (int* p1 = &p.x) { fixed (double* p2 = &array[5]) { // Do something with p1 and p2. } }
![]() |
---|
无法修改在 fixed 语句中初始化的指针。 |
在不安全模式中,可以在堆栈上分配内存。堆栈不受垃圾回收的制约,因此不需要被锁定。有关更多信息,请参见 stackalloc。
// statements_fixed.cs // compile with: /unsafe using System; class Point { public int x, y; } class FixedTest { // Unsafe method: takes a pointer to an int. unsafe static void SquarePtrParam (int* p) { *p *= *p; } unsafe static void Main() { Point pt = new Point(); pt.x = 5; pt.y = 6; // Pin pt in place: fixed (int* p = &pt.x) { SquarePtrParam (p); } // pt now unpinned Console.WriteLine ("{0} {1}", pt.x, pt.y); } }
输出
25 6