fixed 语句禁止垃圾回收器重定位可移动的变量。 fixed 语句只在 不安全的上下文中是允许的。 Fixed 还可用于创建 固定大小缓冲区

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

unsafe static void TestMethod()
{ // Assume that the following class exists.
//class Point
//{
// public int x;
// public int y;
//} // Variable 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 that it is not relocated. fixed (int* p = &pt.x)
{
*p = ;
} }
 

可通过使用数组、字符串、大小固定的缓冲区或变量地址初始化指针。 以下示例演示可变地址、数组和字符串的用法。 关于固定大小缓冲器的更多信息,请参见固定大小的缓冲区(C# 编程指南)

 

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

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

要初始化不同类型的指针,只需嵌套 fixed 语句,如下面的示例所示。

fixed (int* p1 = &point.x) 

    fixed (double* p2 = &arr[5]) 
    { 
        // Do something with p1 and p2. 
    } 
}

执行完语句中的代码后,任何固定变量都被解除固定并受垃圾回收的制约。 因此,不要指向 fixed 语句之外的那些变量。

static unsafe void Test2()
{
Point point = new Point();
double[] arr = { , 1.5, 2.3, 3.4, 4.0, 5.9 };
string str = "Hello World"; // The following two assignments are equivalent. Each assigns the address
// of the first element in array arr to pointer p. // You can initialize a pointer by using an array.
fixed (double* p = arr) { /*...*/ } // You can initialize a pointer by using the address of a variable.
fixed (double* p = &arr[]) { /*...*/ } // The following assignment initializes p by using a string.
fixed (char* p = str) { /*...*/ } // The following assignment is not valid, because str[0] is a char,
// which is a value, not a variable.
//fixed (char* p = &str[0]) { /*...*/ } // You can initialize a pointer by using the address of a variable, such
// as point.x or arr[5].
fixed (int* p1 = &point.x)
{
fixed (double* p2 = &arr[])
{
// Do something with p1 and p2.
}
}
}
注意

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

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

class Point
{
public int x, y;
}
class FixedTest2
{
// 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 = ;
pt.y = ;
// Pin pt in place:
fixed (int* p = &pt.x)
{
SquarePtrParam (p);
}
// pt now unpinned.
Console.WriteLine ("{0} {1}", pt.x, pt.y);
}
}
/*
Output:
25 6
*/

C#fixed关键字的更多相关文章

  1. C# 指针(unsafe与fixed的使用)

    c#在默认情况下生成的都是安全代码,即进行了代码托管(.NET的CLR机制好处之一是,进行代码托管,适时的释放内存,程序员便不必考虑资源的回收问题),而此时,指针不能出现在安全代码的编译条件下. 一. ...

  2. C#中的Unsafe和Fixed

    托管代码 (managed code):由公共语言运行库环境(而不是直接由操作系统)执行的代码.托管代码应用程序可以获得公共语言运行库服务,例如自动垃圾回收.运行库类型检查和安全支持等.这些服务帮助提 ...

  3. C# fixed详解

    相信很多人在这样或那样的项目中,或者无意间看到了fixed语句块,看到之后你肯定会疑问: 1.这个fixed关键字是做什么用的? 2.什么情况下需要该关键字? 3.这个关键字该怎么用? 我相信解决了上 ...

  4. C# fixed语句固定变量详解

    相信很多人在这样或那样的项目中,或者无意间看到了fixed语句块,看到之后你肯定会疑问: 1,这个fixed关键字是做什么用的? 2,什么情况下需要该关键字? 3,这个关键字该怎么用? 我相信解决了上 ...

  5. C#技术漫谈之垃圾回收机制(GC)

    GC的前世与今生 虽然本文是以.NET作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由鼎鼎大名的图林奖得主John McCarthy所实现的Lisp语言就已经提供了GC的功能,这是 ...

  6. cout输出控制——位数和精度控制

    刷到一道需要控制输出精度和位数的题目 刚开始以为单纯使用 iomanip 函数库里的 setprecision 就可以,但 OJ 给我判了答案错误,后来一想这样输出并不能限制位数只能限制有效位数. 比 ...

  7. C#中指针的用法

    (*) unsafe 和 fixed unsafe { ]; ; i < array.Length; i++) { array[i] = i; } fixed (int* p = array) ...

  8. [.net 面向对象编程基础] (7) 基础中的基础——流程控制语句

    [.net 面向对象编程基础] (7) 基础中的基础——流程控制语句 本来没有这一节的内容,后来考虑到既然是一个系列文章,那么就尽可能写的详细一些,本节参考了网上朋友所写的例子,为的是让更多小伙伴学习 ...

  9. 继续SecureString

    上回写了关于SecureString的特征和为什么我们要使用它,这篇继续研究研究这个SecureString. **主要内容:** - SecureString与String之间的转换 - Secur ...

随机推荐

  1. poj 1364 King(差分约束)

    题意(真坑):傻国王只会求和,以及比较大小.阴谋家们想推翻他,于是想坑他,上交了一串长度为n的序列a[1],a[2]...a[n],国王作出m条形如(a[si]+a[si+1]+...+a[si+ni ...

  2. 【C#学习笔记】获得本机IP

    using System; using System.Net; namespace ConsoleApplication { class Program { static void Main(stri ...

  3. 利用ffmpeg解码h264流的代码

    这里也直接给出代码: h264dec.h: #pragma once #include "tdll.h" #include "avcodec.h" #inclu ...

  4. H.264码流结构解析

    from:http://wenku.baidu.com/link?url=hYQHJcAWUIS-8C7nSBbf-8lGagYGXKb5msVwQKWyXFAcPLU5gR4BKOVLrFOw4bX ...

  5. java中时间格式yyyyMMddHHmmss的大小写问题

    字母     日期或时间元素 表示 示例 G Era 标志符 Text AD y 年 Year 1996 ; 96 M 年中的月份 Month July ; Jul ; 07 w 年中的周数 Numb ...

  6. Linux常用设置

    1.文件夹操作 创建-->mkdir NAME 删除-->rm NAME -i 删除前逐一询问确认 -f 直接删除,不确认 -r 将目录即以下档案逐一删除 例:删除所有C语言程序文档,删除 ...

  7. 【Unity3D】生成工程报错解决—UnityEditor.HostView:OnGUI() Error building Player: Couldn't build player because of unsupported data on target platform.

    错误 错误1:An asset is marked as dont save, but is included in the build: unityEditor.HostView:OnGUI() 错 ...

  8. 【转】apue《UNIX环境高级编程第三版》第一章答案详解

    原文网址:http://blog.csdn.net/hubbybob1/article/details/40859835 大家好,从这周开始学习apue<UNIX环境高级编程第三版>,在此 ...

  9. Oracle 课程八之性能优化之10046事件

    Oracle 的事件很多. 具体参考blog: Oracle 跟踪事件 set event 转摘:http://blog.csdn.net/tianlesoftware/archive/2009/12 ...

  10. java web 学习十二(session)

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...