码迷,mamicode.com
首页 > 编程语言 > 详细

c# 使用c++指针

时间:2016-01-16 19:21:42      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

  c++指针基本使用需要指定unsafe编译选项。

using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication1
{
    internal class Program
    {
        unsafe private static void Main(string[] args)
        {
            int x = 10;
            short y = -1;
            byte y2 = 4;
            double z = 1.5;
            int* p = &x;
            short* py = &y;
            double* pz = &z;

            Console.WriteLine("x {0},{1},{2}", (uint)&x, sizeof(int), x);
            Console.WriteLine("y {0},{1},{2}", (uint)&y, sizeof(short), y);
            Console.WriteLine("y2 {0},{1},{2}", (uint)&y2, sizeof(byte), y2);
            Console.WriteLine("z {0},{1},{2}", (uint)&z, sizeof(double), z);
            Console.WriteLine("p {0},{1},{2}", (uint)&p, sizeof(int*), (uint)p);
            Console.WriteLine("py {0},{1},{2}", (uint)&py, sizeof(short*), (uint)py);
            Console.WriteLine("pz {0},{1},{2}", (uint)&pz, sizeof(double*), (uint)pz);

            *p = 20;
            Console.WriteLine("x {0}", x);
            Console.WriteLine("p {0}", *p);

            //强制转换后结果不确定 这个和unsafe没有关系 c++的指针本来就这不能这样操作
            pz = (double*)p;
            Console.WriteLine("pz {0}",*pz);
        }
    }
}

  结构体为非托管类型,类为托管类型,类的地址会由于gc的操作发生改变,需要使用fixed。

using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication1
{
    internal class Program
    {
        internal struct CurrencyStruct
        {
            public long dollars;
            public byte cents;

            public override string ToString()
            {
                return string.Format("Doolars: {0}, Cents: {1}", dollars, cents);
            }
        }

        internal class CurrenyClass
        {
            public long dollars;
            public byte cents;

            public override string ToString()
            {
                return string.Format("Dollars: {0}, Cents: {1}", dollars, cents);
            }
        }



        unsafe private static void Main(string[] args)
        {
            Console.WriteLine(sizeof(CurrencyStruct));

            CurrencyStruct amount1, amount2;
            CurrencyStruct* pAmount = &amount1;
            long* pDollars = &(pAmount->dollars);
            byte* pCents = &(pAmount->cents);

            Console.WriteLine("{0}", (uint)&amount1);
            Console.WriteLine("{0}", (uint)&amount2);
            Console.WriteLine("{0}", (uint)&pAmount);
            Console.WriteLine("{0}", (uint)&pDollars);
            Console.WriteLine("{0}", (uint)&pCents);

            pAmount->dollars = 20;
            *pCents = 50;
            Console.WriteLine(amount1);

            //指向amount2
            pAmount--;
            Console.WriteLine("{0}, {1}", (uint)pAmount, *pAmount);

            //指向amount2的pcents
            CurrencyStruct* pTempCurrency = (CurrencyStruct*) pCents;
            pCents = (byte*) (--pTempCurrency);
            Console.WriteLine("{0}", (uint)&pCents);

            CurrenyClass amount3 = new CurrenyClass();

            fixed(long* pDollar3 = &(amount3.dollars))
            fixed(byte* pCents3 = &(amount3.cents))
            {
                Console.WriteLine("{0}", (uint)pDollar3);
                Console.WriteLine("{0}", (uint)pCents3);

                *pDollar3 = -100;
                Console.WriteLine("{0}", amount3);
            }


        }
    }
}

在栈中创建高性能、低开销的数组

  

using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication1
{
    internal class Program
    {

        private const int Len = 40;
        unsafe private static void Main(string[] args)
        {
            long* pArray = stackalloc long[Len];

            for (int i = 0; i < Len; i++)
            {
                pArray[i] = i * i;
            }


            for (int i = 0; i < Len; i++)
            {
                Console.WriteLine("{0}", pArray[i]);
            }
        }
    }
}

 

c# 使用c++指针

标签:

原文地址:http://www.cnblogs.com/zkzk945/p/5135994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!