C# unsafe代码处理数据

来源:互联网 发布:scratch少儿趣味编程 编辑:程序博客网 时间:2024/05/20 20:22
        public static unsafe int[] add(int[] a)        {            int* s = stackalloc int[100];            int count = a.Length;            fixed (int* p = a)            {                int* q = p;                //for (int i = 0; i < count; i++)                //{                //    q[i]++;                //}                for (int i = 0; i < count; i++)                {                    *q = *q + 1;                    q++;                }            }            return a;        }               //fixed 因为.net 是托管内存,所以用fixed锁定变量内存地址,以便指针访问。        //stackalloc 用来在堆栈划分内存使用空间。

原创粉丝点击