C#指针 unsafe与fixed

  当你喜欢一样东西的时候就你不会去想他是否带来利益,这才是真正的喜欢。

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

  一、unsafe 

如果因需要想在c#中使用指针,那么unsafe便是一个通道(当然在使用前,需在项目属性的生成选项中,选择“允许不安全代码”)。

example 1:

a) 创建项目,项目属性->生成->选择“允许不安全代码”

b) 编写代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleDll
{
//此处为c++动态库的调用,因 DllC++.dll是示例测试文件,为不影响代码演示,此处屏蔽 
//public class Code 
//{ 
// [DllImport("DllC++.dll")] 
// public unsafe static extern int Add(int a, int* b); 
//} 

class Program
{
    public unsafe static int Add(int a, int* b)//此处使用 指针,需要加入非安全代码关键字unsafe 
    {
        return a + *b;
    }

    static void Main(string[] args)
    {    

        unsafe//此代码块为非安全代码,可以使用指针 
        {
            int resDll,resInner, a;
            int* b;//如果在安全代码条件下,编译不会通过对指针的定义 
            a = 1;
            b = &a;
            // resDll = Code.Add(a, b); 
            resInner = Add(a,b);
        }

    }

}

二、fixed

  fixed其实是在非安全代码下用到才会有意义的。因为他负责对指针所指向的“动态分配内存的对象或对象中的成员”简称“对象”进行锁定(之所以要锁 定,是因为即使在非安全代码模式下,这些对象一样的使用CLR的代码托管,这样的话,很可能造成对象的地址变动,就是因为CLR对资源重新分配的不确定 性,假使指针在未完成对对象的操作时,对象地址变动,那么指针指向的地址就会出现混乱,很可能造成内存泄漏甚至系统崩溃。如果使用fixed进行锁定,那 么只有在执行完fixed模块后,指针所指向的对象才能被移动)。

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;



namespace ConsoleDll
{

    public static int Add(int[] a,int b)
    {
        unsafe
        {
            fixed (int* pa = a)//此处将锁住a,使得在fixed操作块内,a不会被CLR移动 
            {
                return *pa + b;
            }
        }
    }

    static void Main(string[] args)
    {

        int[] a = new int[1];
        a[0] = 1;
        int b = 2;
        int res = Add(a,b);

    }
}

  pa的地址也是被固定了的,所以不能对他进行赋值操作!

转载自:http://www.360doc.com/content/10/0618/16/1472642_33815702.shtml

原文地址:https://www.cnblogs.com/cxeye/p/2711972.html