Esempio n. 1
0
        public static unsafe MyStruct *Allocate(MyStruct MyStruct)
        {
            MyStruct *MyStructHandlerPointer = (MyStruct *)Marshal.AllocHGlobal(sizeof(MyStruct));

            *MyStructHandlerPointer = MyStruct;
            return(MyStructHandlerPointer);
        }
Esempio n. 2
0
        /// <summary>
        /// 引用类型的指针
        /// </summary>
        public static void Demo2()
        {
            //结构指针
            MyStruct  aStruct = new MyStruct();
            MyStruct *pStruct = &aStruct;

            (*pStruct).F = 3.4F;
            (*pStruct).x = 11;

            pStruct->F = 3.5F;
            pStruct->x = 2;

            long *pLong = &(pStruct->x);


            //类成员指针
            Test myClass = new Test();

            /*//会产生编码错误,以下写法
             * long* pLong1 = &(myClass.X);*/

            fixed(float *pFloat1 = &(myClass.F))
            fixed(long *pLong1 = &(myClass.X))
            {
            }
        }
    static void Main(string[] args)
    {
        MyStruct mc = new MyStruct(10);

        // Will print 10
        Console.WriteLine(mc.i);
        unsafe
        {
            MyStruct *pmc = &mc;
            // Will print the address of mc
            Console.WriteLine((IntPtr)pmc);
        }

        // Assign to mc
        mc = new MyStruct(20);

        // Will print 20; mc was modified
        Console.WriteLine(mc.i);
        unsafe
        {
            MyStruct *p = &mc;
            // Will print the same address as above.
            // mc was modified in place
            // because structs have value semantics
            Console.WriteLine((IntPtr)p);
        }
    }
        unsafe static void Main(string[] args)
        {
            MyStruct  ms; //Not necessary to instantiate aan instance of a struct with new keyword
            MyStruct *ms1 = &ms;

            ms1->SetXY(10, 20);
            ms1->ShowXY();
        }
Esempio n. 5
0
    static void unsafe Main(string[] args)
    {
        int       x     = 10;
        int *     ptr1  = &x;
        int       y     = *ptr1;
        MyStruct  st    = new MyStruct();
        MyStruct *ptrst = &st;

        ptrstr->a();
    }
Esempio n. 6
0
    static void Main()
    {
        MyClass  o = new MyClass();
        MyStruct s = new MyStruct();

        unsafe
        {
            fixed(int *p = &o.fld1, q = &o.fld2)
            {
                *         p = 0;
                *         q = 0;
                MyStruct *r = &s;

                (*r).fld = 0;
            }
        }

        int[] arr = { 1, 2, 3 };
        addone(arr);
    }
Esempio n. 7
0
        private void Start()
        {
            using (UnsafeListV2 <MyStruct> warmList = new UnsafeListV2 <MyStruct>(1, 1))
            {
                MyStruct *MyStructPtr = MyStruct.Allocate(new MyStruct(1, true, 5));
                warmList.Add(MyStructPtr);
                Marshal.FreeHGlobal((IntPtr)MyStructPtr);
            }


            Profiler.BeginSample("UnsafeTest : Allocation");
            var list = new UnsafeListV2 <MyStruct>(1024, 2);

            for (int i = 0; i < 1024; i++)
            {
                MyStruct *MyStructPtrTmp = MyStruct.Allocate(new MyStruct(1, true, 5));
                list.Add(MyStructPtrTmp);
            }

            Profiler.EndSample();


            Profiler.BeginSample("UnsafeTest : DeAlloc");
            for (int i = 0; i < 1000; i++)
            {
                var foundPtr = list.Get(i);
                if ((IntPtr)foundPtr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal((IntPtr)foundPtr);
                }
            }

            list.Dispose();

            Profiler.EndSample();
        }