zoukankan      html  css  js  c++  java
  • C#不安全代码和Fixed

    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 = 1;
        }        
    
    }

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

    static unsafe void Test2()
    {
        Point point = new Point();
        double[] arr = { 0, 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[0]) { /*...*/ }
    
        // 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[5])
            {
                // Do something with p1 and p2.
            }
        }
    }

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

    fixed (byte* ps = srcarray, pd = dstarray) {...}
    要初始化不同类型的指针,只需嵌套 fixed 语句,如下面的示例所示。
    fixed (int* p1 = &point.x)
    {
        fixed (double* p2 = &arr[5])
        {
            // Do something with p1 and p2.
        }
    }

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

    注意:无法修改在 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 = 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);
            }
        }
        /*
        Output:
        25 6
         */
  • 相关阅读:
    Vue框架(三)——Vue项目搭建和项目目录介绍、组件、路由
    Vue框架(二)——Vue指令(v-once指令、v-cloak指令、条件指令、v-pre指令、循环指令)、todolist案例、Vue实例(计算、监听)、组件、组件数据交互
    Vue框架(一)——Vue导读、Vue实例(挂载点el、数据data、过滤器filters)、Vue指令(文本指令v-text、事件指令v-on、属性指令v-bind、表单指令v-model)
    异步调用与回调机制
    进程池与线程池
    多线程实现并发的套接字通信
    线程queue
    定时器
    Event事件
    信号量
  • 原文地址:https://www.cnblogs.com/2Yous/p/4888513.html
Copyright © 2011-2022 走看看